]>
Commit | Line | Data |
---|---|---|
6f97dba0 AL |
1 | /* |
2 | * QEMU System Emulator | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | #include "qemu-common.h" | |
83c9089e | 25 | #include "monitor/monitor.h" |
28ecbaee | 26 | #include "ui/console.h" |
9c17d615 | 27 | #include "sysemu/sysemu.h" |
1de7afc9 | 28 | #include "qemu/timer.h" |
927d4878 | 29 | #include "char/char.h" |
cf3ebac7 | 30 | #include "hw/usb.h" |
c5a415a0 | 31 | #include "qmp-commands.h" |
6f97dba0 AL |
32 | |
33 | #include <unistd.h> | |
34 | #include <fcntl.h> | |
6f97dba0 AL |
35 | #include <time.h> |
36 | #include <errno.h> | |
37 | #include <sys/time.h> | |
38 | #include <zlib.h> | |
39 | ||
40 | #ifndef _WIN32 | |
41 | #include <sys/times.h> | |
42 | #include <sys/wait.h> | |
43 | #include <termios.h> | |
44 | #include <sys/mman.h> | |
45 | #include <sys/ioctl.h> | |
24646c7e | 46 | #include <sys/resource.h> |
6f97dba0 AL |
47 | #include <sys/socket.h> |
48 | #include <netinet/in.h> | |
24646c7e | 49 | #include <net/if.h> |
24646c7e | 50 | #include <arpa/inet.h> |
6f97dba0 AL |
51 | #include <dirent.h> |
52 | #include <netdb.h> | |
53 | #include <sys/select.h> | |
71e72a19 | 54 | #ifdef CONFIG_BSD |
6f97dba0 | 55 | #include <sys/stat.h> |
a167ba50 AJ |
56 | #if defined(__GLIBC__) |
57 | #include <pty.h> | |
3294ce18 MT |
58 | #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
59 | #include <libutil.h> | |
60 | #else | |
61 | #include <util.h> | |
a167ba50 | 62 | #endif |
3294ce18 MT |
63 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
64 | #include <dev/ppbus/ppi.h> | |
65 | #include <dev/ppbus/ppbconf.h> | |
c5e97233 | 66 | #elif defined(__DragonFly__) |
c5e97233 BS |
67 | #include <dev/misc/ppi/ppi.h> |
68 | #include <bus/ppbus/ppbconf.h> | |
6f97dba0 | 69 | #endif |
bbe813a2 | 70 | #else |
6f97dba0 | 71 | #ifdef __linux__ |
6f97dba0 AL |
72 | #include <pty.h> |
73 | ||
74 | #include <linux/ppdev.h> | |
75 | #include <linux/parport.h> | |
76 | #endif | |
77 | #ifdef __sun__ | |
78 | #include <sys/stat.h> | |
79 | #include <sys/ethernet.h> | |
80 | #include <sys/sockio.h> | |
81 | #include <netinet/arp.h> | |
82 | #include <netinet/in.h> | |
83 | #include <netinet/in_systm.h> | |
84 | #include <netinet/ip.h> | |
85 | #include <netinet/ip_icmp.h> // must come after ip.h | |
86 | #include <netinet/udp.h> | |
87 | #include <netinet/tcp.h> | |
88 | #include <net/if.h> | |
89 | #include <syslog.h> | |
90 | #include <stropts.h> | |
91 | #endif | |
92 | #endif | |
93 | #endif | |
94 | ||
1de7afc9 | 95 | #include "qemu/sockets.h" |
cbcc6336 | 96 | #include "ui/qemu-spice.h" |
6f97dba0 | 97 | |
9bd7854e AS |
98 | #define READ_BUF_LEN 4096 |
99 | ||
6f97dba0 AL |
100 | /***********************************************************/ |
101 | /* character device */ | |
102 | ||
72cf2d4f BS |
103 | static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs = |
104 | QTAILQ_HEAD_INITIALIZER(chardevs); | |
2970a6c9 | 105 | |
a425d23f | 106 | void qemu_chr_be_event(CharDriverState *s, int event) |
6f97dba0 | 107 | { |
73cdf3f2 AG |
108 | /* Keep track if the char device is open */ |
109 | switch (event) { | |
110 | case CHR_EVENT_OPENED: | |
16665b94 | 111 | s->be_open = 1; |
73cdf3f2 AG |
112 | break; |
113 | case CHR_EVENT_CLOSED: | |
16665b94 | 114 | s->be_open = 0; |
73cdf3f2 AG |
115 | break; |
116 | } | |
117 | ||
6f97dba0 AL |
118 | if (!s->chr_event) |
119 | return; | |
120 | s->chr_event(s->handler_opaque, event); | |
121 | } | |
122 | ||
fee204fd | 123 | static gboolean qemu_chr_be_generic_open_bh(gpointer opaque) |
6f97dba0 AL |
124 | { |
125 | CharDriverState *s = opaque; | |
a425d23f | 126 | qemu_chr_be_event(s, CHR_EVENT_OPENED); |
9f939df9 AL |
127 | s->idle_tag = 0; |
128 | return FALSE; | |
6f97dba0 AL |
129 | } |
130 | ||
fee204fd | 131 | void qemu_chr_be_generic_open(CharDriverState *s) |
6f97dba0 | 132 | { |
9f939df9 | 133 | if (s->idle_tag == 0) { |
fee204fd | 134 | s->idle_tag = g_idle_add(qemu_chr_be_generic_open_bh, s); |
6f97dba0 AL |
135 | } |
136 | } | |
137 | ||
2cc6e0a1 | 138 | int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len) |
6f97dba0 AL |
139 | { |
140 | return s->chr_write(s, buf, len); | |
141 | } | |
142 | ||
cd18720a AL |
143 | int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len) |
144 | { | |
145 | int offset = 0; | |
146 | int res; | |
147 | ||
148 | while (offset < len) { | |
149 | do { | |
150 | res = s->chr_write(s, buf + offset, len - offset); | |
151 | if (res == -1 && errno == EAGAIN) { | |
152 | g_usleep(100); | |
153 | } | |
154 | } while (res == -1 && errno == EAGAIN); | |
155 | ||
156 | if (res == 0) { | |
157 | break; | |
158 | } | |
159 | ||
160 | if (res < 0) { | |
161 | return res; | |
162 | } | |
163 | ||
164 | offset += res; | |
165 | } | |
166 | ||
167 | return offset; | |
168 | } | |
169 | ||
41084f1b | 170 | int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg) |
6f97dba0 AL |
171 | { |
172 | if (!s->chr_ioctl) | |
173 | return -ENOTSUP; | |
174 | return s->chr_ioctl(s, cmd, arg); | |
175 | } | |
176 | ||
909cda12 | 177 | int qemu_chr_be_can_write(CharDriverState *s) |
6f97dba0 AL |
178 | { |
179 | if (!s->chr_can_read) | |
180 | return 0; | |
181 | return s->chr_can_read(s->handler_opaque); | |
182 | } | |
183 | ||
fa5efccb | 184 | void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len) |
6f97dba0 | 185 | { |
ac310734 SW |
186 | if (s->chr_read) { |
187 | s->chr_read(s->handler_opaque, buf, len); | |
188 | } | |
6f97dba0 AL |
189 | } |
190 | ||
74c0d6f0 | 191 | int qemu_chr_fe_get_msgfd(CharDriverState *s) |
7d174059 MM |
192 | { |
193 | return s->get_msgfd ? s->get_msgfd(s) : -1; | |
194 | } | |
195 | ||
13661089 DB |
196 | int qemu_chr_add_client(CharDriverState *s, int fd) |
197 | { | |
198 | return s->chr_add_client ? s->chr_add_client(s, fd) : -1; | |
199 | } | |
200 | ||
6f97dba0 AL |
201 | void qemu_chr_accept_input(CharDriverState *s) |
202 | { | |
203 | if (s->chr_accept_input) | |
204 | s->chr_accept_input(s); | |
98c8ee1d | 205 | qemu_notify_event(); |
6f97dba0 AL |
206 | } |
207 | ||
e7e71b0e | 208 | void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...) |
6f97dba0 | 209 | { |
9bd7854e | 210 | char buf[READ_BUF_LEN]; |
6f97dba0 AL |
211 | va_list ap; |
212 | va_start(ap, fmt); | |
213 | vsnprintf(buf, sizeof(buf), fmt, ap); | |
2cc6e0a1 | 214 | qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf)); |
6f97dba0 AL |
215 | va_end(ap); |
216 | } | |
217 | ||
6f97dba0 | 218 | void qemu_chr_add_handlers(CharDriverState *s, |
7b27a769 | 219 | IOCanReadHandler *fd_can_read, |
6f97dba0 AL |
220 | IOReadHandler *fd_read, |
221 | IOEventHandler *fd_event, | |
222 | void *opaque) | |
223 | { | |
19083228 HG |
224 | int fe_open; |
225 | ||
da7d998b | 226 | if (!opaque && !fd_can_read && !fd_read && !fd_event) { |
19083228 HG |
227 | fe_open = 0; |
228 | } else { | |
229 | fe_open = 1; | |
2d6c1ef4 | 230 | } |
6f97dba0 AL |
231 | s->chr_can_read = fd_can_read; |
232 | s->chr_read = fd_read; | |
233 | s->chr_event = fd_event; | |
234 | s->handler_opaque = opaque; | |
235 | if (s->chr_update_read_handler) | |
236 | s->chr_update_read_handler(s); | |
73cdf3f2 | 237 | |
19083228 | 238 | if (!s->explicit_fe_open) { |
8e25daa8 | 239 | qemu_chr_fe_set_open(s, fe_open); |
19083228 HG |
240 | } |
241 | ||
73cdf3f2 AG |
242 | /* We're connecting to an already opened device, so let's make sure we |
243 | also get the open event */ | |
a59bcd31 | 244 | if (fe_open && s->be_open) { |
fee204fd | 245 | qemu_chr_be_generic_open(s); |
73cdf3f2 | 246 | } |
6f97dba0 AL |
247 | } |
248 | ||
249 | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) | |
250 | { | |
251 | return len; | |
252 | } | |
253 | ||
80dca9e6 | 254 | static CharDriverState *qemu_chr_open_null(void) |
6f97dba0 AL |
255 | { |
256 | CharDriverState *chr; | |
257 | ||
7267c094 | 258 | chr = g_malloc0(sizeof(CharDriverState)); |
6f97dba0 | 259 | chr->chr_write = null_chr_write; |
1f51470d | 260 | return chr; |
6f97dba0 AL |
261 | } |
262 | ||
263 | /* MUX driver for serial I/O splitting */ | |
6f97dba0 AL |
264 | #define MAX_MUX 4 |
265 | #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */ | |
266 | #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1) | |
267 | typedef struct { | |
7b27a769 | 268 | IOCanReadHandler *chr_can_read[MAX_MUX]; |
6f97dba0 AL |
269 | IOReadHandler *chr_read[MAX_MUX]; |
270 | IOEventHandler *chr_event[MAX_MUX]; | |
271 | void *ext_opaque[MAX_MUX]; | |
272 | CharDriverState *drv; | |
799f1f23 | 273 | int focus; |
6f97dba0 AL |
274 | int mux_cnt; |
275 | int term_got_escape; | |
276 | int max_size; | |
a80bf99f AL |
277 | /* Intermediate input buffer allows to catch escape sequences even if the |
278 | currently active device is not accepting any input - but only until it | |
279 | is full as well. */ | |
280 | unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE]; | |
281 | int prod[MAX_MUX]; | |
282 | int cons[MAX_MUX]; | |
2d22959d | 283 | int timestamps; |
4ab312f7 | 284 | int linestart; |
2d22959d | 285 | int64_t timestamps_start; |
6f97dba0 AL |
286 | } MuxDriver; |
287 | ||
288 | ||
289 | static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len) | |
290 | { | |
291 | MuxDriver *d = chr->opaque; | |
292 | int ret; | |
2d22959d | 293 | if (!d->timestamps) { |
6f97dba0 AL |
294 | ret = d->drv->chr_write(d->drv, buf, len); |
295 | } else { | |
296 | int i; | |
297 | ||
298 | ret = 0; | |
4ab312f7 JK |
299 | for (i = 0; i < len; i++) { |
300 | if (d->linestart) { | |
6f97dba0 AL |
301 | char buf1[64]; |
302 | int64_t ti; | |
303 | int secs; | |
304 | ||
7bd427d8 | 305 | ti = qemu_get_clock_ms(rt_clock); |
2d22959d JK |
306 | if (d->timestamps_start == -1) |
307 | d->timestamps_start = ti; | |
308 | ti -= d->timestamps_start; | |
a4bb1db8 | 309 | secs = ti / 1000; |
6f97dba0 AL |
310 | snprintf(buf1, sizeof(buf1), |
311 | "[%02d:%02d:%02d.%03d] ", | |
312 | secs / 3600, | |
313 | (secs / 60) % 60, | |
314 | secs % 60, | |
a4bb1db8 | 315 | (int)(ti % 1000)); |
6f97dba0 | 316 | d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1)); |
4ab312f7 JK |
317 | d->linestart = 0; |
318 | } | |
319 | ret += d->drv->chr_write(d->drv, buf+i, 1); | |
320 | if (buf[i] == '\n') { | |
321 | d->linestart = 1; | |
6f97dba0 AL |
322 | } |
323 | } | |
324 | } | |
325 | return ret; | |
326 | } | |
327 | ||
328 | static const char * const mux_help[] = { | |
329 | "% h print this help\n\r", | |
330 | "% x exit emulator\n\r", | |
331 | "% s save disk data back to file (if -snapshot)\n\r", | |
332 | "% t toggle console timestamps\n\r" | |
333 | "% b send break (magic sysrq)\n\r", | |
334 | "% c switch between console and monitor\n\r", | |
335 | "% % sends %\n\r", | |
336 | NULL | |
337 | }; | |
338 | ||
339 | int term_escape_char = 0x01; /* ctrl-a is used for escape */ | |
340 | static void mux_print_help(CharDriverState *chr) | |
341 | { | |
342 | int i, j; | |
343 | char ebuf[15] = "Escape-Char"; | |
344 | char cbuf[50] = "\n\r"; | |
345 | ||
346 | if (term_escape_char > 0 && term_escape_char < 26) { | |
347 | snprintf(cbuf, sizeof(cbuf), "\n\r"); | |
348 | snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a'); | |
349 | } else { | |
350 | snprintf(cbuf, sizeof(cbuf), | |
351 | "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", | |
352 | term_escape_char); | |
353 | } | |
354 | chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf)); | |
355 | for (i = 0; mux_help[i] != NULL; i++) { | |
356 | for (j=0; mux_help[i][j] != '\0'; j++) { | |
357 | if (mux_help[i][j] == '%') | |
358 | chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf)); | |
359 | else | |
360 | chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1); | |
361 | } | |
362 | } | |
363 | } | |
364 | ||
2724b180 AL |
365 | static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event) |
366 | { | |
367 | if (d->chr_event[mux_nr]) | |
368 | d->chr_event[mux_nr](d->ext_opaque[mux_nr], event); | |
369 | } | |
370 | ||
6f97dba0 AL |
371 | static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch) |
372 | { | |
373 | if (d->term_got_escape) { | |
374 | d->term_got_escape = 0; | |
375 | if (ch == term_escape_char) | |
376 | goto send_char; | |
377 | switch(ch) { | |
378 | case '?': | |
379 | case 'h': | |
380 | mux_print_help(chr); | |
381 | break; | |
382 | case 'x': | |
383 | { | |
384 | const char *term = "QEMU: Terminated\n\r"; | |
385 | chr->chr_write(chr,(uint8_t *)term,strlen(term)); | |
386 | exit(0); | |
387 | break; | |
388 | } | |
389 | case 's': | |
6ab4b5ab | 390 | bdrv_commit_all(); |
6f97dba0 AL |
391 | break; |
392 | case 'b': | |
a425d23f | 393 | qemu_chr_be_event(chr, CHR_EVENT_BREAK); |
6f97dba0 AL |
394 | break; |
395 | case 'c': | |
396 | /* Switch to the next registered device */ | |
799f1f23 GH |
397 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); |
398 | d->focus++; | |
399 | if (d->focus >= d->mux_cnt) | |
400 | d->focus = 0; | |
401 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN); | |
6f97dba0 | 402 | break; |
2d22959d JK |
403 | case 't': |
404 | d->timestamps = !d->timestamps; | |
405 | d->timestamps_start = -1; | |
4ab312f7 | 406 | d->linestart = 0; |
2d22959d | 407 | break; |
6f97dba0 AL |
408 | } |
409 | } else if (ch == term_escape_char) { | |
410 | d->term_got_escape = 1; | |
411 | } else { | |
412 | send_char: | |
413 | return 1; | |
414 | } | |
415 | return 0; | |
416 | } | |
417 | ||
418 | static void mux_chr_accept_input(CharDriverState *chr) | |
419 | { | |
6f97dba0 | 420 | MuxDriver *d = chr->opaque; |
799f1f23 | 421 | int m = d->focus; |
6f97dba0 | 422 | |
a80bf99f | 423 | while (d->prod[m] != d->cons[m] && |
6f97dba0 AL |
424 | d->chr_can_read[m] && |
425 | d->chr_can_read[m](d->ext_opaque[m])) { | |
426 | d->chr_read[m](d->ext_opaque[m], | |
a80bf99f | 427 | &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1); |
6f97dba0 AL |
428 | } |
429 | } | |
430 | ||
431 | static int mux_chr_can_read(void *opaque) | |
432 | { | |
433 | CharDriverState *chr = opaque; | |
434 | MuxDriver *d = chr->opaque; | |
799f1f23 | 435 | int m = d->focus; |
6f97dba0 | 436 | |
a80bf99f | 437 | if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE) |
6f97dba0 | 438 | return 1; |
a80bf99f AL |
439 | if (d->chr_can_read[m]) |
440 | return d->chr_can_read[m](d->ext_opaque[m]); | |
6f97dba0 AL |
441 | return 0; |
442 | } | |
443 | ||
444 | static void mux_chr_read(void *opaque, const uint8_t *buf, int size) | |
445 | { | |
446 | CharDriverState *chr = opaque; | |
447 | MuxDriver *d = chr->opaque; | |
799f1f23 | 448 | int m = d->focus; |
6f97dba0 AL |
449 | int i; |
450 | ||
451 | mux_chr_accept_input (opaque); | |
452 | ||
453 | for(i = 0; i < size; i++) | |
454 | if (mux_proc_byte(chr, d, buf[i])) { | |
a80bf99f | 455 | if (d->prod[m] == d->cons[m] && |
6f97dba0 AL |
456 | d->chr_can_read[m] && |
457 | d->chr_can_read[m](d->ext_opaque[m])) | |
458 | d->chr_read[m](d->ext_opaque[m], &buf[i], 1); | |
459 | else | |
a80bf99f | 460 | d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i]; |
6f97dba0 AL |
461 | } |
462 | } | |
463 | ||
464 | static void mux_chr_event(void *opaque, int event) | |
465 | { | |
466 | CharDriverState *chr = opaque; | |
467 | MuxDriver *d = chr->opaque; | |
468 | int i; | |
469 | ||
470 | /* Send the event to all registered listeners */ | |
471 | for (i = 0; i < d->mux_cnt; i++) | |
2724b180 | 472 | mux_chr_send_event(d, i, event); |
6f97dba0 AL |
473 | } |
474 | ||
475 | static void mux_chr_update_read_handler(CharDriverState *chr) | |
476 | { | |
477 | MuxDriver *d = chr->opaque; | |
478 | ||
479 | if (d->mux_cnt >= MAX_MUX) { | |
480 | fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n"); | |
481 | return; | |
482 | } | |
483 | d->ext_opaque[d->mux_cnt] = chr->handler_opaque; | |
484 | d->chr_can_read[d->mux_cnt] = chr->chr_can_read; | |
485 | d->chr_read[d->mux_cnt] = chr->chr_read; | |
486 | d->chr_event[d->mux_cnt] = chr->chr_event; | |
487 | /* Fix up the real driver with mux routines */ | |
488 | if (d->mux_cnt == 0) { | |
489 | qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read, | |
490 | mux_chr_event, chr); | |
491 | } | |
799f1f23 GH |
492 | if (d->focus != -1) { |
493 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); | |
a7aec5da | 494 | } |
799f1f23 | 495 | d->focus = d->mux_cnt; |
6f97dba0 | 496 | d->mux_cnt++; |
799f1f23 | 497 | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN); |
6f97dba0 AL |
498 | } |
499 | ||
500 | static CharDriverState *qemu_chr_open_mux(CharDriverState *drv) | |
501 | { | |
502 | CharDriverState *chr; | |
503 | MuxDriver *d; | |
504 | ||
7267c094 AL |
505 | chr = g_malloc0(sizeof(CharDriverState)); |
506 | d = g_malloc0(sizeof(MuxDriver)); | |
6f97dba0 AL |
507 | |
508 | chr->opaque = d; | |
509 | d->drv = drv; | |
799f1f23 | 510 | d->focus = -1; |
6f97dba0 AL |
511 | chr->chr_write = mux_chr_write; |
512 | chr->chr_update_read_handler = mux_chr_update_read_handler; | |
513 | chr->chr_accept_input = mux_chr_accept_input; | |
7c32c4fe | 514 | /* Frontend guest-open / -close notification is not support with muxes */ |
574b711a | 515 | chr->chr_set_fe_open = NULL; |
73cdf3f2 AG |
516 | |
517 | /* Muxes are always open on creation */ | |
fee204fd | 518 | qemu_chr_be_generic_open(chr); |
73cdf3f2 | 519 | |
6f97dba0 AL |
520 | return chr; |
521 | } | |
522 | ||
523 | ||
524 | #ifdef _WIN32 | |
d247d25f | 525 | int send_all(int fd, const void *buf, int len1) |
6f97dba0 AL |
526 | { |
527 | int ret, len; | |
528 | ||
529 | len = len1; | |
530 | while (len > 0) { | |
531 | ret = send(fd, buf, len, 0); | |
532 | if (ret < 0) { | |
6f97dba0 AL |
533 | errno = WSAGetLastError(); |
534 | if (errno != WSAEWOULDBLOCK) { | |
535 | return -1; | |
536 | } | |
537 | } else if (ret == 0) { | |
538 | break; | |
539 | } else { | |
540 | buf += ret; | |
541 | len -= ret; | |
542 | } | |
543 | } | |
544 | return len1 - len; | |
545 | } | |
546 | ||
547 | #else | |
548 | ||
5fc9cfed | 549 | int send_all(int fd, const void *_buf, int len1) |
6f97dba0 AL |
550 | { |
551 | int ret, len; | |
5fc9cfed | 552 | const uint8_t *buf = _buf; |
6f97dba0 AL |
553 | |
554 | len = len1; | |
555 | while (len > 0) { | |
556 | ret = write(fd, buf, len); | |
557 | if (ret < 0) { | |
558 | if (errno != EINTR && errno != EAGAIN) | |
559 | return -1; | |
560 | } else if (ret == 0) { | |
561 | break; | |
562 | } else { | |
563 | buf += ret; | |
564 | len -= ret; | |
565 | } | |
566 | } | |
567 | return len1 - len; | |
568 | } | |
4549a8b7 SB |
569 | |
570 | int recv_all(int fd, void *_buf, int len1, bool single_read) | |
571 | { | |
572 | int ret, len; | |
573 | uint8_t *buf = _buf; | |
574 | ||
575 | len = len1; | |
576 | while ((len > 0) && (ret = read(fd, buf, len)) != 0) { | |
577 | if (ret < 0) { | |
578 | if (errno != EINTR && errno != EAGAIN) { | |
579 | return -1; | |
580 | } | |
581 | continue; | |
582 | } else { | |
583 | if (single_read) { | |
584 | return ret; | |
585 | } | |
586 | buf += ret; | |
587 | len -= ret; | |
588 | } | |
589 | } | |
590 | return len1 - len; | |
591 | } | |
592 | ||
6f97dba0 AL |
593 | #endif /* !_WIN32 */ |
594 | ||
96c63847 AL |
595 | typedef struct IOWatchPoll |
596 | { | |
d185c094 PB |
597 | GSource parent; |
598 | ||
1e885b25 | 599 | GIOChannel *channel; |
96c63847 | 600 | GSource *src; |
96c63847 AL |
601 | |
602 | IOCanReadHandler *fd_can_read; | |
1e885b25 | 603 | GSourceFunc fd_read; |
96c63847 | 604 | void *opaque; |
96c63847 AL |
605 | } IOWatchPoll; |
606 | ||
96c63847 AL |
607 | static IOWatchPoll *io_watch_poll_from_source(GSource *source) |
608 | { | |
d185c094 | 609 | return container_of(source, IOWatchPoll, parent); |
96c63847 AL |
610 | } |
611 | ||
612 | static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_) | |
613 | { | |
614 | IOWatchPoll *iwp = io_watch_poll_from_source(source); | |
d185c094 | 615 | bool now_active = iwp->fd_can_read(iwp->opaque) > 0; |
1e885b25 | 616 | bool was_active = iwp->src != NULL; |
d185c094 | 617 | if (was_active == now_active) { |
96c63847 AL |
618 | return FALSE; |
619 | } | |
620 | ||
d185c094 | 621 | if (now_active) { |
1e885b25 PB |
622 | iwp->src = g_io_create_watch(iwp->channel, G_IO_IN | G_IO_ERR | G_IO_HUP); |
623 | g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL); | |
d185c094 PB |
624 | g_source_attach(iwp->src, NULL); |
625 | } else { | |
1e885b25 PB |
626 | g_source_destroy(iwp->src); |
627 | g_source_unref(iwp->src); | |
628 | iwp->src = NULL; | |
d185c094 PB |
629 | } |
630 | return FALSE; | |
96c63847 AL |
631 | } |
632 | ||
633 | static gboolean io_watch_poll_check(GSource *source) | |
634 | { | |
d185c094 | 635 | return FALSE; |
96c63847 AL |
636 | } |
637 | ||
638 | static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback, | |
639 | gpointer user_data) | |
640 | { | |
d185c094 | 641 | abort(); |
96c63847 AL |
642 | } |
643 | ||
644 | static void io_watch_poll_finalize(GSource *source) | |
645 | { | |
646 | IOWatchPoll *iwp = io_watch_poll_from_source(source); | |
1e885b25 | 647 | g_source_destroy(iwp->src); |
d185c094 | 648 | g_source_unref(iwp->src); |
1e885b25 | 649 | iwp->src = NULL; |
96c63847 AL |
650 | } |
651 | ||
652 | static GSourceFuncs io_watch_poll_funcs = { | |
653 | .prepare = io_watch_poll_prepare, | |
654 | .check = io_watch_poll_check, | |
655 | .dispatch = io_watch_poll_dispatch, | |
656 | .finalize = io_watch_poll_finalize, | |
657 | }; | |
658 | ||
659 | /* Can only be used for read */ | |
660 | static guint io_add_watch_poll(GIOChannel *channel, | |
661 | IOCanReadHandler *fd_can_read, | |
662 | GIOFunc fd_read, | |
663 | gpointer user_data) | |
664 | { | |
665 | IOWatchPoll *iwp; | |
96c63847 | 666 | |
d185c094 | 667 | iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs, sizeof(IOWatchPoll)); |
96c63847 AL |
668 | iwp->fd_can_read = fd_can_read; |
669 | iwp->opaque = user_data; | |
1e885b25 PB |
670 | iwp->channel = channel; |
671 | iwp->fd_read = (GSourceFunc) fd_read; | |
672 | iwp->src = NULL; | |
96c63847 | 673 | |
d185c094 | 674 | return g_source_attach(&iwp->parent, NULL); |
96c63847 AL |
675 | } |
676 | ||
44ab9ed4 | 677 | #ifndef _WIN32 |
96c63847 AL |
678 | static GIOChannel *io_channel_from_fd(int fd) |
679 | { | |
680 | GIOChannel *chan; | |
681 | ||
682 | if (fd == -1) { | |
683 | return NULL; | |
684 | } | |
685 | ||
686 | chan = g_io_channel_unix_new(fd); | |
687 | ||
688 | g_io_channel_set_encoding(chan, NULL, NULL); | |
689 | g_io_channel_set_buffered(chan, FALSE); | |
690 | ||
691 | return chan; | |
692 | } | |
44ab9ed4 | 693 | #endif |
96c63847 | 694 | |
76a9644b AL |
695 | static GIOChannel *io_channel_from_socket(int fd) |
696 | { | |
697 | GIOChannel *chan; | |
698 | ||
699 | if (fd == -1) { | |
700 | return NULL; | |
701 | } | |
702 | ||
703 | #ifdef _WIN32 | |
704 | chan = g_io_channel_win32_new_socket(fd); | |
705 | #else | |
706 | chan = g_io_channel_unix_new(fd); | |
707 | #endif | |
708 | ||
709 | g_io_channel_set_encoding(chan, NULL, NULL); | |
710 | g_io_channel_set_buffered(chan, FALSE); | |
711 | ||
712 | return chan; | |
713 | } | |
714 | ||
684a096e | 715 | static int io_channel_send(GIOChannel *fd, const void *buf, size_t len) |
96c63847 AL |
716 | { |
717 | GIOStatus status; | |
684a096e | 718 | size_t offset; |
96c63847 | 719 | |
684a096e AL |
720 | offset = 0; |
721 | while (offset < len) { | |
722 | gsize bytes_written; | |
723 | ||
724 | status = g_io_channel_write_chars(fd, buf + offset, len - offset, | |
96c63847 AL |
725 | &bytes_written, NULL); |
726 | if (status != G_IO_STATUS_NORMAL) { | |
23673ca7 | 727 | if (status == G_IO_STATUS_AGAIN) { |
684a096e AL |
728 | /* If we've written any data, return a partial write. */ |
729 | if (offset) { | |
730 | break; | |
731 | } | |
23673ca7 | 732 | errno = EAGAIN; |
23673ca7 AL |
733 | } else { |
734 | errno = EINVAL; | |
96c63847 | 735 | } |
684a096e AL |
736 | |
737 | return -1; | |
96c63847 AL |
738 | } else if (status == G_IO_STATUS_EOF) { |
739 | break; | |
96c63847 | 740 | } |
684a096e AL |
741 | |
742 | offset += bytes_written; | |
96c63847 | 743 | } |
684a096e AL |
744 | |
745 | return offset; | |
96c63847 | 746 | } |
96c63847 | 747 | |
44ab9ed4 BS |
748 | #ifndef _WIN32 |
749 | ||
a29753f8 AL |
750 | typedef struct FDCharDriver { |
751 | CharDriverState *chr; | |
752 | GIOChannel *fd_in, *fd_out; | |
753 | guint fd_in_tag; | |
6f97dba0 | 754 | int max_size; |
a29753f8 | 755 | QTAILQ_ENTRY(FDCharDriver) node; |
6f97dba0 AL |
756 | } FDCharDriver; |
757 | ||
6f97dba0 AL |
758 | static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
759 | { | |
760 | FDCharDriver *s = chr->opaque; | |
a29753f8 | 761 | |
684a096e | 762 | return io_channel_send(s->fd_out, buf, len); |
6f97dba0 AL |
763 | } |
764 | ||
a29753f8 | 765 | static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) |
6f97dba0 AL |
766 | { |
767 | CharDriverState *chr = opaque; | |
768 | FDCharDriver *s = chr->opaque; | |
a29753f8 | 769 | int len; |
9bd7854e | 770 | uint8_t buf[READ_BUF_LEN]; |
a29753f8 AL |
771 | GIOStatus status; |
772 | gsize bytes_read; | |
6f97dba0 AL |
773 | |
774 | len = sizeof(buf); | |
a29753f8 | 775 | if (len > s->max_size) { |
6f97dba0 | 776 | len = s->max_size; |
a29753f8 AL |
777 | } |
778 | if (len == 0) { | |
779 | return FALSE; | |
780 | } | |
781 | ||
782 | status = g_io_channel_read_chars(chan, (gchar *)buf, | |
783 | len, &bytes_read, NULL); | |
784 | if (status == G_IO_STATUS_EOF) { | |
a425d23f | 785 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
a29753f8 | 786 | return FALSE; |
6f97dba0 | 787 | } |
a29753f8 AL |
788 | if (status == G_IO_STATUS_NORMAL) { |
789 | qemu_chr_be_write(chr, buf, bytes_read); | |
6f97dba0 | 790 | } |
a29753f8 AL |
791 | |
792 | return TRUE; | |
793 | } | |
794 | ||
795 | static int fd_chr_read_poll(void *opaque) | |
796 | { | |
797 | CharDriverState *chr = opaque; | |
798 | FDCharDriver *s = chr->opaque; | |
799 | ||
800 | s->max_size = qemu_chr_be_can_write(chr); | |
801 | return s->max_size; | |
6f97dba0 AL |
802 | } |
803 | ||
23673ca7 AL |
804 | static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond) |
805 | { | |
806 | FDCharDriver *s = chr->opaque; | |
807 | return g_io_create_watch(s->fd_out, cond); | |
808 | } | |
809 | ||
6f97dba0 AL |
810 | static void fd_chr_update_read_handler(CharDriverState *chr) |
811 | { | |
812 | FDCharDriver *s = chr->opaque; | |
813 | ||
a29753f8 AL |
814 | if (s->fd_in_tag) { |
815 | g_source_remove(s->fd_in_tag); | |
816 | } | |
817 | ||
818 | if (s->fd_in) { | |
819 | s->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll, fd_chr_read, chr); | |
6f97dba0 AL |
820 | } |
821 | } | |
822 | ||
823 | static void fd_chr_close(struct CharDriverState *chr) | |
824 | { | |
825 | FDCharDriver *s = chr->opaque; | |
826 | ||
a29753f8 AL |
827 | if (s->fd_in_tag) { |
828 | g_source_remove(s->fd_in_tag); | |
829 | s->fd_in_tag = 0; | |
830 | } | |
831 | ||
832 | if (s->fd_in) { | |
833 | g_io_channel_unref(s->fd_in); | |
834 | } | |
835 | if (s->fd_out) { | |
836 | g_io_channel_unref(s->fd_out); | |
6f97dba0 AL |
837 | } |
838 | ||
7267c094 | 839 | g_free(s); |
a425d23f | 840 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
841 | } |
842 | ||
843 | /* open a character device to a unix fd */ | |
844 | static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) | |
845 | { | |
846 | CharDriverState *chr; | |
847 | FDCharDriver *s; | |
848 | ||
7267c094 AL |
849 | chr = g_malloc0(sizeof(CharDriverState)); |
850 | s = g_malloc0(sizeof(FDCharDriver)); | |
a29753f8 AL |
851 | s->fd_in = io_channel_from_fd(fd_in); |
852 | s->fd_out = io_channel_from_fd(fd_out); | |
23673ca7 | 853 | fcntl(fd_out, F_SETFL, O_NONBLOCK); |
a29753f8 | 854 | s->chr = chr; |
6f97dba0 | 855 | chr->opaque = s; |
23673ca7 | 856 | chr->chr_add_watch = fd_chr_add_watch; |
6f97dba0 AL |
857 | chr->chr_write = fd_chr_write; |
858 | chr->chr_update_read_handler = fd_chr_update_read_handler; | |
859 | chr->chr_close = fd_chr_close; | |
860 | ||
fee204fd | 861 | qemu_chr_be_generic_open(chr); |
6f97dba0 AL |
862 | |
863 | return chr; | |
864 | } | |
865 | ||
548cbb36 | 866 | static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts) |
6f97dba0 AL |
867 | { |
868 | int fd_in, fd_out; | |
869 | char filename_in[256], filename_out[256]; | |
548cbb36 | 870 | const char *filename = opts->device; |
7d31544f GH |
871 | |
872 | if (filename == NULL) { | |
873 | fprintf(stderr, "chardev: pipe: no filename given\n"); | |
1f51470d | 874 | return NULL; |
7d31544f | 875 | } |
6f97dba0 AL |
876 | |
877 | snprintf(filename_in, 256, "%s.in", filename); | |
878 | snprintf(filename_out, 256, "%s.out", filename); | |
40ff6d7e KW |
879 | TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY)); |
880 | TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY)); | |
6f97dba0 AL |
881 | if (fd_in < 0 || fd_out < 0) { |
882 | if (fd_in >= 0) | |
883 | close(fd_in); | |
884 | if (fd_out >= 0) | |
885 | close(fd_out); | |
b181e047 | 886 | TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY)); |
a89dd6c3 | 887 | if (fd_in < 0) { |
1f51470d | 888 | return NULL; |
a89dd6c3 | 889 | } |
6f97dba0 | 890 | } |
1f51470d | 891 | return qemu_chr_open_fd(fd_in, fd_out); |
6f97dba0 AL |
892 | } |
893 | ||
6f97dba0 AL |
894 | /* init terminal so that we can grab keys */ |
895 | static struct termios oldtty; | |
896 | static int old_fd0_flags; | |
bb002513 | 897 | static bool stdio_allow_signal; |
6f97dba0 AL |
898 | |
899 | static void term_exit(void) | |
900 | { | |
901 | tcsetattr (0, TCSANOW, &oldtty); | |
902 | fcntl(0, F_SETFL, old_fd0_flags); | |
903 | } | |
904 | ||
bb002513 | 905 | static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo) |
6f97dba0 AL |
906 | { |
907 | struct termios tty; | |
908 | ||
0369364b | 909 | tty = oldtty; |
bb002513 PB |
910 | if (!echo) { |
911 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP | |
6f97dba0 | 912 | |INLCR|IGNCR|ICRNL|IXON); |
bb002513 PB |
913 | tty.c_oflag |= OPOST; |
914 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); | |
915 | tty.c_cflag &= ~(CSIZE|PARENB); | |
916 | tty.c_cflag |= CS8; | |
917 | tty.c_cc[VMIN] = 1; | |
918 | tty.c_cc[VTIME] = 0; | |
919 | } | |
6f97dba0 | 920 | /* if graphical mode, we allow Ctrl-C handling */ |
bb002513 | 921 | if (!stdio_allow_signal) |
6f97dba0 | 922 | tty.c_lflag &= ~ISIG; |
6f97dba0 AL |
923 | |
924 | tcsetattr (0, TCSANOW, &tty); | |
6f97dba0 AL |
925 | } |
926 | ||
927 | static void qemu_chr_close_stdio(struct CharDriverState *chr) | |
928 | { | |
929 | term_exit(); | |
6f97dba0 AL |
930 | fd_chr_close(chr); |
931 | } | |
932 | ||
7c358031 | 933 | static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts) |
6f97dba0 AL |
934 | { |
935 | CharDriverState *chr; | |
936 | ||
ab51b1d5 MT |
937 | if (is_daemonized()) { |
938 | error_report("cannot use stdio with -daemonize"); | |
939 | return NULL; | |
940 | } | |
ed7a1540 AL |
941 | old_fd0_flags = fcntl(0, F_GETFL); |
942 | tcgetattr (0, &oldtty); | |
943 | fcntl(0, F_SETFL, O_NONBLOCK); | |
944 | atexit(term_exit); | |
0369364b | 945 | |
6f97dba0 AL |
946 | chr = qemu_chr_open_fd(0, 1); |
947 | chr->chr_close = qemu_chr_close_stdio; | |
bb002513 | 948 | chr->chr_set_echo = qemu_chr_set_echo_stdio; |
7c358031 GH |
949 | stdio_allow_signal = display_type != DT_NOGRAPHIC; |
950 | if (opts->has_signal) { | |
951 | stdio_allow_signal = opts->signal; | |
952 | } | |
15f31519 | 953 | qemu_chr_fe_set_echo(chr, false); |
6f97dba0 | 954 | |
1f51470d | 955 | return chr; |
6f97dba0 AL |
956 | } |
957 | ||
958 | #ifdef __sun__ | |
959 | /* Once Solaris has openpty(), this is going to be removed. */ | |
3f4cb3d3 BS |
960 | static int openpty(int *amaster, int *aslave, char *name, |
961 | struct termios *termp, struct winsize *winp) | |
6f97dba0 AL |
962 | { |
963 | const char *slave; | |
964 | int mfd = -1, sfd = -1; | |
965 | ||
966 | *amaster = *aslave = -1; | |
967 | ||
968 | mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY); | |
969 | if (mfd < 0) | |
970 | goto err; | |
971 | ||
972 | if (grantpt(mfd) == -1 || unlockpt(mfd) == -1) | |
973 | goto err; | |
974 | ||
975 | if ((slave = ptsname(mfd)) == NULL) | |
976 | goto err; | |
977 | ||
978 | if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1) | |
979 | goto err; | |
980 | ||
981 | if (ioctl(sfd, I_PUSH, "ptem") == -1 || | |
982 | (termp != NULL && tcgetattr(sfd, termp) < 0)) | |
983 | goto err; | |
984 | ||
985 | if (amaster) | |
986 | *amaster = mfd; | |
987 | if (aslave) | |
988 | *aslave = sfd; | |
989 | if (winp) | |
990 | ioctl(sfd, TIOCSWINSZ, winp); | |
991 | ||
992 | return 0; | |
993 | ||
994 | err: | |
995 | if (sfd != -1) | |
996 | close(sfd); | |
997 | close(mfd); | |
998 | return -1; | |
999 | } | |
1000 | ||
3f4cb3d3 | 1001 | static void cfmakeraw (struct termios *termios_p) |
6f97dba0 AL |
1002 | { |
1003 | termios_p->c_iflag &= | |
1004 | ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); | |
1005 | termios_p->c_oflag &= ~OPOST; | |
1006 | termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); | |
1007 | termios_p->c_cflag &= ~(CSIZE|PARENB); | |
1008 | termios_p->c_cflag |= CS8; | |
1009 | ||
1010 | termios_p->c_cc[VMIN] = 0; | |
1011 | termios_p->c_cc[VTIME] = 0; | |
1012 | } | |
1013 | #endif | |
1014 | ||
1015 | #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \ | |
a167ba50 AJ |
1016 | || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \ |
1017 | || defined(__GLIBC__) | |
6f97dba0 | 1018 | |
e551498e GH |
1019 | #define HAVE_CHARDEV_TTY 1 |
1020 | ||
6f97dba0 | 1021 | typedef struct { |
093d3a20 AL |
1022 | GIOChannel *fd; |
1023 | guint fd_tag; | |
6f97dba0 AL |
1024 | int connected; |
1025 | int polling; | |
1026 | int read_bytes; | |
8aa33caf | 1027 | guint timer_tag; |
6f97dba0 AL |
1028 | } PtyCharDriver; |
1029 | ||
1030 | static void pty_chr_update_read_handler(CharDriverState *chr); | |
1031 | static void pty_chr_state(CharDriverState *chr, int connected); | |
1032 | ||
8aa33caf AL |
1033 | static gboolean pty_chr_timer(gpointer opaque) |
1034 | { | |
1035 | struct CharDriverState *chr = opaque; | |
1036 | PtyCharDriver *s = chr->opaque; | |
1037 | ||
1038 | if (s->connected) { | |
1039 | goto out; | |
1040 | } | |
1041 | if (s->polling) { | |
1042 | /* If we arrive here without polling being cleared due | |
1043 | * read returning -EIO, then we are (re-)connected */ | |
1044 | pty_chr_state(chr, 1); | |
1045 | goto out; | |
1046 | } | |
1047 | ||
1048 | /* Next poll ... */ | |
1049 | pty_chr_update_read_handler(chr); | |
1050 | ||
1051 | out: | |
1052 | return FALSE; | |
1053 | } | |
1054 | ||
1055 | static void pty_chr_rearm_timer(CharDriverState *chr, int ms) | |
1056 | { | |
1057 | PtyCharDriver *s = chr->opaque; | |
1058 | ||
1059 | if (s->timer_tag) { | |
1060 | g_source_remove(s->timer_tag); | |
1061 | s->timer_tag = 0; | |
1062 | } | |
1063 | ||
1064 | if (ms == 1000) { | |
1065 | s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr); | |
1066 | } else { | |
1067 | s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr); | |
1068 | } | |
1069 | } | |
1070 | ||
6f97dba0 AL |
1071 | static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1072 | { | |
1073 | PtyCharDriver *s = chr->opaque; | |
1074 | ||
1075 | if (!s->connected) { | |
1076 | /* guest sends data, check for (re-)connect */ | |
1077 | pty_chr_update_read_handler(chr); | |
1078 | return 0; | |
1079 | } | |
684a096e | 1080 | return io_channel_send(s->fd, buf, len); |
6f97dba0 AL |
1081 | } |
1082 | ||
e6a87ed8 AL |
1083 | static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond) |
1084 | { | |
1085 | PtyCharDriver *s = chr->opaque; | |
1086 | return g_io_create_watch(s->fd, cond); | |
1087 | } | |
1088 | ||
6f97dba0 AL |
1089 | static int pty_chr_read_poll(void *opaque) |
1090 | { | |
1091 | CharDriverState *chr = opaque; | |
1092 | PtyCharDriver *s = chr->opaque; | |
1093 | ||
909cda12 | 1094 | s->read_bytes = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
1095 | return s->read_bytes; |
1096 | } | |
1097 | ||
093d3a20 | 1098 | static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) |
6f97dba0 AL |
1099 | { |
1100 | CharDriverState *chr = opaque; | |
1101 | PtyCharDriver *s = chr->opaque; | |
093d3a20 | 1102 | gsize size, len; |
9bd7854e | 1103 | uint8_t buf[READ_BUF_LEN]; |
093d3a20 | 1104 | GIOStatus status; |
6f97dba0 AL |
1105 | |
1106 | len = sizeof(buf); | |
1107 | if (len > s->read_bytes) | |
1108 | len = s->read_bytes; | |
1109 | if (len == 0) | |
093d3a20 AL |
1110 | return FALSE; |
1111 | status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL); | |
1112 | if (status != G_IO_STATUS_NORMAL) { | |
6f97dba0 | 1113 | pty_chr_state(chr, 0); |
093d3a20 AL |
1114 | return FALSE; |
1115 | } else { | |
6f97dba0 | 1116 | pty_chr_state(chr, 1); |
fa5efccb | 1117 | qemu_chr_be_write(chr, buf, size); |
6f97dba0 | 1118 | } |
093d3a20 | 1119 | return TRUE; |
6f97dba0 AL |
1120 | } |
1121 | ||
1122 | static void pty_chr_update_read_handler(CharDriverState *chr) | |
1123 | { | |
1124 | PtyCharDriver *s = chr->opaque; | |
1125 | ||
093d3a20 AL |
1126 | if (s->fd_tag) { |
1127 | g_source_remove(s->fd_tag); | |
1128 | } | |
1129 | ||
1130 | s->fd_tag = io_add_watch_poll(s->fd, pty_chr_read_poll, pty_chr_read, chr); | |
6f97dba0 AL |
1131 | s->polling = 1; |
1132 | /* | |
1133 | * Short timeout here: just need wait long enougth that qemu makes | |
1134 | * it through the poll loop once. When reconnected we want a | |
1135 | * short timeout so we notice it almost instantly. Otherwise | |
1136 | * read() gives us -EIO instantly, making pty_chr_state() reset the | |
1137 | * timeout to the normal (much longer) poll interval before the | |
1138 | * timer triggers. | |
1139 | */ | |
8aa33caf | 1140 | pty_chr_rearm_timer(chr, 10); |
6f97dba0 AL |
1141 | } |
1142 | ||
1143 | static void pty_chr_state(CharDriverState *chr, int connected) | |
1144 | { | |
1145 | PtyCharDriver *s = chr->opaque; | |
1146 | ||
1147 | if (!connected) { | |
093d3a20 AL |
1148 | g_source_remove(s->fd_tag); |
1149 | s->fd_tag = 0; | |
6f97dba0 AL |
1150 | s->connected = 0; |
1151 | s->polling = 0; | |
1152 | /* (re-)connect poll interval for idle guests: once per second. | |
1153 | * We check more frequently in case the guests sends data to | |
1154 | * the virtual device linked to our pty. */ | |
8aa33caf | 1155 | pty_chr_rearm_timer(chr, 1000); |
6f97dba0 AL |
1156 | } else { |
1157 | if (!s->connected) | |
fee204fd | 1158 | qemu_chr_be_generic_open(chr); |
6f97dba0 AL |
1159 | s->connected = 1; |
1160 | } | |
1161 | } | |
1162 | ||
6f97dba0 AL |
1163 | |
1164 | static void pty_chr_close(struct CharDriverState *chr) | |
1165 | { | |
1166 | PtyCharDriver *s = chr->opaque; | |
093d3a20 | 1167 | int fd; |
6f97dba0 | 1168 | |
093d3a20 AL |
1169 | if (s->fd_tag) { |
1170 | g_source_remove(s->fd_tag); | |
1171 | } | |
1172 | fd = g_io_channel_unix_get_fd(s->fd); | |
1173 | g_io_channel_unref(s->fd); | |
1174 | close(fd); | |
8aa33caf AL |
1175 | if (s->timer_tag) { |
1176 | g_source_remove(s->timer_tag); | |
1177 | } | |
7267c094 | 1178 | g_free(s); |
a425d23f | 1179 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
1180 | } |
1181 | ||
e68c5958 GH |
1182 | static CharDriverState *qemu_chr_open_pty(const char *id, |
1183 | ChardevReturn *ret) | |
6f97dba0 AL |
1184 | { |
1185 | CharDriverState *chr; | |
1186 | PtyCharDriver *s; | |
1187 | struct termios tty; | |
e68c5958 | 1188 | int master_fd, slave_fd; |
c5e97233 | 1189 | #if defined(__OpenBSD__) || defined(__DragonFly__) |
6f97dba0 AL |
1190 | char pty_name[PATH_MAX]; |
1191 | #define q_ptsname(x) pty_name | |
1192 | #else | |
1193 | char *pty_name = NULL; | |
1194 | #define q_ptsname(x) ptsname(x) | |
1195 | #endif | |
1196 | ||
a4e26048 | 1197 | if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) { |
1f51470d | 1198 | return NULL; |
6f97dba0 AL |
1199 | } |
1200 | ||
1201 | /* Set raw attributes on the pty. */ | |
c3b972c3 | 1202 | tcgetattr(slave_fd, &tty); |
6f97dba0 AL |
1203 | cfmakeraw(&tty); |
1204 | tcsetattr(slave_fd, TCSAFLUSH, &tty); | |
1205 | close(slave_fd); | |
1206 | ||
a4e26048 MA |
1207 | chr = g_malloc0(sizeof(CharDriverState)); |
1208 | ||
e68c5958 GH |
1209 | chr->filename = g_strdup_printf("pty:%s", q_ptsname(master_fd)); |
1210 | ret->pty = g_strdup(q_ptsname(master_fd)); | |
1211 | ret->has_pty = true; | |
58650218 | 1212 | |
e68c5958 GH |
1213 | fprintf(stderr, "char device redirected to %s (label %s)\n", |
1214 | q_ptsname(master_fd), id); | |
6f97dba0 | 1215 | |
a4e26048 | 1216 | s = g_malloc0(sizeof(PtyCharDriver)); |
6f97dba0 AL |
1217 | chr->opaque = s; |
1218 | chr->chr_write = pty_chr_write; | |
1219 | chr->chr_update_read_handler = pty_chr_update_read_handler; | |
1220 | chr->chr_close = pty_chr_close; | |
e6a87ed8 | 1221 | chr->chr_add_watch = pty_chr_add_watch; |
6f97dba0 | 1222 | |
093d3a20 | 1223 | s->fd = io_channel_from_fd(master_fd); |
8aa33caf | 1224 | s->timer_tag = 0; |
6f97dba0 | 1225 | |
1f51470d | 1226 | return chr; |
6f97dba0 AL |
1227 | } |
1228 | ||
1229 | static void tty_serial_init(int fd, int speed, | |
1230 | int parity, int data_bits, int stop_bits) | |
1231 | { | |
1232 | struct termios tty; | |
1233 | speed_t spd; | |
1234 | ||
1235 | #if 0 | |
1236 | printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n", | |
1237 | speed, parity, data_bits, stop_bits); | |
1238 | #endif | |
1239 | tcgetattr (fd, &tty); | |
1240 | ||
45eea13b SW |
1241 | #define check_speed(val) if (speed <= val) { spd = B##val; break; } |
1242 | speed = speed * 10 / 11; | |
1243 | do { | |
1244 | check_speed(50); | |
1245 | check_speed(75); | |
1246 | check_speed(110); | |
1247 | check_speed(134); | |
1248 | check_speed(150); | |
1249 | check_speed(200); | |
1250 | check_speed(300); | |
1251 | check_speed(600); | |
1252 | check_speed(1200); | |
1253 | check_speed(1800); | |
1254 | check_speed(2400); | |
1255 | check_speed(4800); | |
1256 | check_speed(9600); | |
1257 | check_speed(19200); | |
1258 | check_speed(38400); | |
1259 | /* Non-Posix values follow. They may be unsupported on some systems. */ | |
1260 | check_speed(57600); | |
1261 | check_speed(115200); | |
1262 | #ifdef B230400 | |
1263 | check_speed(230400); | |
1264 | #endif | |
1265 | #ifdef B460800 | |
1266 | check_speed(460800); | |
1267 | #endif | |
1268 | #ifdef B500000 | |
1269 | check_speed(500000); | |
1270 | #endif | |
1271 | #ifdef B576000 | |
1272 | check_speed(576000); | |
1273 | #endif | |
1274 | #ifdef B921600 | |
1275 | check_speed(921600); | |
1276 | #endif | |
1277 | #ifdef B1000000 | |
1278 | check_speed(1000000); | |
1279 | #endif | |
1280 | #ifdef B1152000 | |
1281 | check_speed(1152000); | |
1282 | #endif | |
1283 | #ifdef B1500000 | |
1284 | check_speed(1500000); | |
1285 | #endif | |
1286 | #ifdef B2000000 | |
1287 | check_speed(2000000); | |
1288 | #endif | |
1289 | #ifdef B2500000 | |
1290 | check_speed(2500000); | |
1291 | #endif | |
1292 | #ifdef B3000000 | |
1293 | check_speed(3000000); | |
1294 | #endif | |
1295 | #ifdef B3500000 | |
1296 | check_speed(3500000); | |
1297 | #endif | |
1298 | #ifdef B4000000 | |
1299 | check_speed(4000000); | |
1300 | #endif | |
6f97dba0 | 1301 | spd = B115200; |
45eea13b | 1302 | } while (0); |
6f97dba0 AL |
1303 | |
1304 | cfsetispeed(&tty, spd); | |
1305 | cfsetospeed(&tty, spd); | |
1306 | ||
1307 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP | |
1308 | |INLCR|IGNCR|ICRNL|IXON); | |
1309 | tty.c_oflag |= OPOST; | |
1310 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); | |
1311 | tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); | |
1312 | switch(data_bits) { | |
1313 | default: | |
1314 | case 8: | |
1315 | tty.c_cflag |= CS8; | |
1316 | break; | |
1317 | case 7: | |
1318 | tty.c_cflag |= CS7; | |
1319 | break; | |
1320 | case 6: | |
1321 | tty.c_cflag |= CS6; | |
1322 | break; | |
1323 | case 5: | |
1324 | tty.c_cflag |= CS5; | |
1325 | break; | |
1326 | } | |
1327 | switch(parity) { | |
1328 | default: | |
1329 | case 'N': | |
1330 | break; | |
1331 | case 'E': | |
1332 | tty.c_cflag |= PARENB; | |
1333 | break; | |
1334 | case 'O': | |
1335 | tty.c_cflag |= PARENB | PARODD; | |
1336 | break; | |
1337 | } | |
1338 | if (stop_bits == 2) | |
1339 | tty.c_cflag |= CSTOPB; | |
1340 | ||
1341 | tcsetattr (fd, TCSANOW, &tty); | |
1342 | } | |
1343 | ||
1344 | static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) | |
1345 | { | |
1346 | FDCharDriver *s = chr->opaque; | |
1347 | ||
1348 | switch(cmd) { | |
1349 | case CHR_IOCTL_SERIAL_SET_PARAMS: | |
1350 | { | |
1351 | QEMUSerialSetParams *ssp = arg; | |
a29753f8 AL |
1352 | tty_serial_init(g_io_channel_unix_get_fd(s->fd_in), |
1353 | ssp->speed, ssp->parity, | |
6f97dba0 AL |
1354 | ssp->data_bits, ssp->stop_bits); |
1355 | } | |
1356 | break; | |
1357 | case CHR_IOCTL_SERIAL_SET_BREAK: | |
1358 | { | |
1359 | int enable = *(int *)arg; | |
a29753f8 AL |
1360 | if (enable) { |
1361 | tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1); | |
1362 | } | |
6f97dba0 AL |
1363 | } |
1364 | break; | |
1365 | case CHR_IOCTL_SERIAL_GET_TIOCM: | |
1366 | { | |
1367 | int sarg = 0; | |
1368 | int *targ = (int *)arg; | |
a29753f8 | 1369 | ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg); |
6f97dba0 | 1370 | *targ = 0; |
b4abdfa4 | 1371 | if (sarg & TIOCM_CTS) |
6f97dba0 | 1372 | *targ |= CHR_TIOCM_CTS; |
b4abdfa4 | 1373 | if (sarg & TIOCM_CAR) |
6f97dba0 | 1374 | *targ |= CHR_TIOCM_CAR; |
b4abdfa4 | 1375 | if (sarg & TIOCM_DSR) |
6f97dba0 | 1376 | *targ |= CHR_TIOCM_DSR; |
b4abdfa4 | 1377 | if (sarg & TIOCM_RI) |
6f97dba0 | 1378 | *targ |= CHR_TIOCM_RI; |
b4abdfa4 | 1379 | if (sarg & TIOCM_DTR) |
6f97dba0 | 1380 | *targ |= CHR_TIOCM_DTR; |
b4abdfa4 | 1381 | if (sarg & TIOCM_RTS) |
6f97dba0 AL |
1382 | *targ |= CHR_TIOCM_RTS; |
1383 | } | |
1384 | break; | |
1385 | case CHR_IOCTL_SERIAL_SET_TIOCM: | |
1386 | { | |
1387 | int sarg = *(int *)arg; | |
1388 | int targ = 0; | |
a29753f8 | 1389 | ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ); |
b4abdfa4 AJ |
1390 | targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR |
1391 | | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS); | |
1392 | if (sarg & CHR_TIOCM_CTS) | |
1393 | targ |= TIOCM_CTS; | |
1394 | if (sarg & CHR_TIOCM_CAR) | |
1395 | targ |= TIOCM_CAR; | |
1396 | if (sarg & CHR_TIOCM_DSR) | |
1397 | targ |= TIOCM_DSR; | |
1398 | if (sarg & CHR_TIOCM_RI) | |
1399 | targ |= TIOCM_RI; | |
1400 | if (sarg & CHR_TIOCM_DTR) | |
6f97dba0 | 1401 | targ |= TIOCM_DTR; |
b4abdfa4 | 1402 | if (sarg & CHR_TIOCM_RTS) |
6f97dba0 | 1403 | targ |= TIOCM_RTS; |
a29753f8 | 1404 | ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ); |
6f97dba0 AL |
1405 | } |
1406 | break; | |
1407 | default: | |
1408 | return -ENOTSUP; | |
1409 | } | |
1410 | return 0; | |
1411 | } | |
1412 | ||
4266a134 DA |
1413 | static void qemu_chr_close_tty(CharDriverState *chr) |
1414 | { | |
1415 | FDCharDriver *s = chr->opaque; | |
1416 | int fd = -1; | |
1417 | ||
1418 | if (s) { | |
a29753f8 | 1419 | fd = g_io_channel_unix_get_fd(s->fd_in); |
4266a134 DA |
1420 | } |
1421 | ||
1422 | fd_chr_close(chr); | |
1423 | ||
1424 | if (fd >= 0) { | |
1425 | close(fd); | |
1426 | } | |
1427 | } | |
1428 | ||
d59044ef GH |
1429 | static CharDriverState *qemu_chr_open_tty_fd(int fd) |
1430 | { | |
1431 | CharDriverState *chr; | |
1432 | ||
1433 | tty_serial_init(fd, 115200, 'N', 8, 1); | |
1434 | chr = qemu_chr_open_fd(fd, fd); | |
1435 | chr->chr_ioctl = tty_serial_ioctl; | |
1436 | chr->chr_close = qemu_chr_close_tty; | |
1437 | return chr; | |
1438 | } | |
6f97dba0 AL |
1439 | #endif /* __linux__ || __sun__ */ |
1440 | ||
1441 | #if defined(__linux__) | |
e551498e GH |
1442 | |
1443 | #define HAVE_CHARDEV_PARPORT 1 | |
1444 | ||
6f97dba0 AL |
1445 | typedef struct { |
1446 | int fd; | |
1447 | int mode; | |
1448 | } ParallelCharDriver; | |
1449 | ||
1450 | static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode) | |
1451 | { | |
1452 | if (s->mode != mode) { | |
1453 | int m = mode; | |
1454 | if (ioctl(s->fd, PPSETMODE, &m) < 0) | |
1455 | return 0; | |
1456 | s->mode = mode; | |
1457 | } | |
1458 | return 1; | |
1459 | } | |
1460 | ||
1461 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) | |
1462 | { | |
1463 | ParallelCharDriver *drv = chr->opaque; | |
1464 | int fd = drv->fd; | |
1465 | uint8_t b; | |
1466 | ||
1467 | switch(cmd) { | |
1468 | case CHR_IOCTL_PP_READ_DATA: | |
1469 | if (ioctl(fd, PPRDATA, &b) < 0) | |
1470 | return -ENOTSUP; | |
1471 | *(uint8_t *)arg = b; | |
1472 | break; | |
1473 | case CHR_IOCTL_PP_WRITE_DATA: | |
1474 | b = *(uint8_t *)arg; | |
1475 | if (ioctl(fd, PPWDATA, &b) < 0) | |
1476 | return -ENOTSUP; | |
1477 | break; | |
1478 | case CHR_IOCTL_PP_READ_CONTROL: | |
1479 | if (ioctl(fd, PPRCONTROL, &b) < 0) | |
1480 | return -ENOTSUP; | |
1481 | /* Linux gives only the lowest bits, and no way to know data | |
1482 | direction! For better compatibility set the fixed upper | |
1483 | bits. */ | |
1484 | *(uint8_t *)arg = b | 0xc0; | |
1485 | break; | |
1486 | case CHR_IOCTL_PP_WRITE_CONTROL: | |
1487 | b = *(uint8_t *)arg; | |
1488 | if (ioctl(fd, PPWCONTROL, &b) < 0) | |
1489 | return -ENOTSUP; | |
1490 | break; | |
1491 | case CHR_IOCTL_PP_READ_STATUS: | |
1492 | if (ioctl(fd, PPRSTATUS, &b) < 0) | |
1493 | return -ENOTSUP; | |
1494 | *(uint8_t *)arg = b; | |
1495 | break; | |
1496 | case CHR_IOCTL_PP_DATA_DIR: | |
1497 | if (ioctl(fd, PPDATADIR, (int *)arg) < 0) | |
1498 | return -ENOTSUP; | |
1499 | break; | |
1500 | case CHR_IOCTL_PP_EPP_READ_ADDR: | |
1501 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { | |
1502 | struct ParallelIOArg *parg = arg; | |
1503 | int n = read(fd, parg->buffer, parg->count); | |
1504 | if (n != parg->count) { | |
1505 | return -EIO; | |
1506 | } | |
1507 | } | |
1508 | break; | |
1509 | case CHR_IOCTL_PP_EPP_READ: | |
1510 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { | |
1511 | struct ParallelIOArg *parg = arg; | |
1512 | int n = read(fd, parg->buffer, parg->count); | |
1513 | if (n != parg->count) { | |
1514 | return -EIO; | |
1515 | } | |
1516 | } | |
1517 | break; | |
1518 | case CHR_IOCTL_PP_EPP_WRITE_ADDR: | |
1519 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { | |
1520 | struct ParallelIOArg *parg = arg; | |
1521 | int n = write(fd, parg->buffer, parg->count); | |
1522 | if (n != parg->count) { | |
1523 | return -EIO; | |
1524 | } | |
1525 | } | |
1526 | break; | |
1527 | case CHR_IOCTL_PP_EPP_WRITE: | |
1528 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { | |
1529 | struct ParallelIOArg *parg = arg; | |
1530 | int n = write(fd, parg->buffer, parg->count); | |
1531 | if (n != parg->count) { | |
1532 | return -EIO; | |
1533 | } | |
1534 | } | |
1535 | break; | |
1536 | default: | |
1537 | return -ENOTSUP; | |
1538 | } | |
1539 | return 0; | |
1540 | } | |
1541 | ||
1542 | static void pp_close(CharDriverState *chr) | |
1543 | { | |
1544 | ParallelCharDriver *drv = chr->opaque; | |
1545 | int fd = drv->fd; | |
1546 | ||
1547 | pp_hw_mode(drv, IEEE1284_MODE_COMPAT); | |
1548 | ioctl(fd, PPRELEASE); | |
1549 | close(fd); | |
7267c094 | 1550 | g_free(drv); |
a425d23f | 1551 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
1552 | } |
1553 | ||
88a946d3 | 1554 | static CharDriverState *qemu_chr_open_pp_fd(int fd) |
6f97dba0 AL |
1555 | { |
1556 | CharDriverState *chr; | |
1557 | ParallelCharDriver *drv; | |
6f97dba0 AL |
1558 | |
1559 | if (ioctl(fd, PPCLAIM) < 0) { | |
1560 | close(fd); | |
1f51470d | 1561 | return NULL; |
6f97dba0 AL |
1562 | } |
1563 | ||
7267c094 | 1564 | drv = g_malloc0(sizeof(ParallelCharDriver)); |
6f97dba0 AL |
1565 | drv->fd = fd; |
1566 | drv->mode = IEEE1284_MODE_COMPAT; | |
1567 | ||
7267c094 | 1568 | chr = g_malloc0(sizeof(CharDriverState)); |
6f97dba0 AL |
1569 | chr->chr_write = null_chr_write; |
1570 | chr->chr_ioctl = pp_ioctl; | |
1571 | chr->chr_close = pp_close; | |
1572 | chr->opaque = drv; | |
1573 | ||
fee204fd | 1574 | qemu_chr_be_generic_open(chr); |
6f97dba0 | 1575 | |
1f51470d | 1576 | return chr; |
6f97dba0 AL |
1577 | } |
1578 | #endif /* __linux__ */ | |
1579 | ||
a167ba50 | 1580 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
e551498e GH |
1581 | |
1582 | #define HAVE_CHARDEV_PARPORT 1 | |
1583 | ||
6972f935 BS |
1584 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
1585 | { | |
e0efb993 | 1586 | int fd = (int)(intptr_t)chr->opaque; |
6972f935 BS |
1587 | uint8_t b; |
1588 | ||
1589 | switch(cmd) { | |
1590 | case CHR_IOCTL_PP_READ_DATA: | |
1591 | if (ioctl(fd, PPIGDATA, &b) < 0) | |
1592 | return -ENOTSUP; | |
1593 | *(uint8_t *)arg = b; | |
1594 | break; | |
1595 | case CHR_IOCTL_PP_WRITE_DATA: | |
1596 | b = *(uint8_t *)arg; | |
1597 | if (ioctl(fd, PPISDATA, &b) < 0) | |
1598 | return -ENOTSUP; | |
1599 | break; | |
1600 | case CHR_IOCTL_PP_READ_CONTROL: | |
1601 | if (ioctl(fd, PPIGCTRL, &b) < 0) | |
1602 | return -ENOTSUP; | |
1603 | *(uint8_t *)arg = b; | |
1604 | break; | |
1605 | case CHR_IOCTL_PP_WRITE_CONTROL: | |
1606 | b = *(uint8_t *)arg; | |
1607 | if (ioctl(fd, PPISCTRL, &b) < 0) | |
1608 | return -ENOTSUP; | |
1609 | break; | |
1610 | case CHR_IOCTL_PP_READ_STATUS: | |
1611 | if (ioctl(fd, PPIGSTATUS, &b) < 0) | |
1612 | return -ENOTSUP; | |
1613 | *(uint8_t *)arg = b; | |
1614 | break; | |
1615 | default: | |
1616 | return -ENOTSUP; | |
1617 | } | |
1618 | return 0; | |
1619 | } | |
1620 | ||
88a946d3 | 1621 | static CharDriverState *qemu_chr_open_pp_fd(int fd) |
6972f935 BS |
1622 | { |
1623 | CharDriverState *chr; | |
6972f935 | 1624 | |
7267c094 | 1625 | chr = g_malloc0(sizeof(CharDriverState)); |
e0efb993 | 1626 | chr->opaque = (void *)(intptr_t)fd; |
6972f935 BS |
1627 | chr->chr_write = null_chr_write; |
1628 | chr->chr_ioctl = pp_ioctl; | |
1f51470d | 1629 | return chr; |
6972f935 BS |
1630 | } |
1631 | #endif | |
1632 | ||
6f97dba0 AL |
1633 | #else /* _WIN32 */ |
1634 | ||
1635 | typedef struct { | |
1636 | int max_size; | |
1637 | HANDLE hcom, hrecv, hsend; | |
1638 | OVERLAPPED orecv, osend; | |
1639 | BOOL fpipe; | |
1640 | DWORD len; | |
1641 | } WinCharState; | |
1642 | ||
db418a0a FC |
1643 | typedef struct { |
1644 | HANDLE hStdIn; | |
1645 | HANDLE hInputReadyEvent; | |
1646 | HANDLE hInputDoneEvent; | |
1647 | HANDLE hInputThread; | |
1648 | uint8_t win_stdio_buf; | |
1649 | } WinStdioCharState; | |
1650 | ||
6f97dba0 AL |
1651 | #define NSENDBUF 2048 |
1652 | #define NRECVBUF 2048 | |
1653 | #define MAXCONNECT 1 | |
1654 | #define NTIMEOUT 5000 | |
1655 | ||
1656 | static int win_chr_poll(void *opaque); | |
1657 | static int win_chr_pipe_poll(void *opaque); | |
1658 | ||
1659 | static void win_chr_close(CharDriverState *chr) | |
1660 | { | |
1661 | WinCharState *s = chr->opaque; | |
1662 | ||
1663 | if (s->hsend) { | |
1664 | CloseHandle(s->hsend); | |
1665 | s->hsend = NULL; | |
1666 | } | |
1667 | if (s->hrecv) { | |
1668 | CloseHandle(s->hrecv); | |
1669 | s->hrecv = NULL; | |
1670 | } | |
1671 | if (s->hcom) { | |
1672 | CloseHandle(s->hcom); | |
1673 | s->hcom = NULL; | |
1674 | } | |
1675 | if (s->fpipe) | |
1676 | qemu_del_polling_cb(win_chr_pipe_poll, chr); | |
1677 | else | |
1678 | qemu_del_polling_cb(win_chr_poll, chr); | |
793cbfb5 | 1679 | |
a425d23f | 1680 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
1681 | } |
1682 | ||
1683 | static int win_chr_init(CharDriverState *chr, const char *filename) | |
1684 | { | |
1685 | WinCharState *s = chr->opaque; | |
1686 | COMMCONFIG comcfg; | |
1687 | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; | |
1688 | COMSTAT comstat; | |
1689 | DWORD size; | |
1690 | DWORD err; | |
1691 | ||
1692 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); | |
1693 | if (!s->hsend) { | |
1694 | fprintf(stderr, "Failed CreateEvent\n"); | |
1695 | goto fail; | |
1696 | } | |
1697 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); | |
1698 | if (!s->hrecv) { | |
1699 | fprintf(stderr, "Failed CreateEvent\n"); | |
1700 | goto fail; | |
1701 | } | |
1702 | ||
1703 | s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, | |
1704 | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); | |
1705 | if (s->hcom == INVALID_HANDLE_VALUE) { | |
1706 | fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError()); | |
1707 | s->hcom = NULL; | |
1708 | goto fail; | |
1709 | } | |
1710 | ||
1711 | if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) { | |
1712 | fprintf(stderr, "Failed SetupComm\n"); | |
1713 | goto fail; | |
1714 | } | |
1715 | ||
1716 | ZeroMemory(&comcfg, sizeof(COMMCONFIG)); | |
1717 | size = sizeof(COMMCONFIG); | |
1718 | GetDefaultCommConfig(filename, &comcfg, &size); | |
1719 | comcfg.dcb.DCBlength = sizeof(DCB); | |
1720 | CommConfigDialog(filename, NULL, &comcfg); | |
1721 | ||
1722 | if (!SetCommState(s->hcom, &comcfg.dcb)) { | |
1723 | fprintf(stderr, "Failed SetCommState\n"); | |
1724 | goto fail; | |
1725 | } | |
1726 | ||
1727 | if (!SetCommMask(s->hcom, EV_ERR)) { | |
1728 | fprintf(stderr, "Failed SetCommMask\n"); | |
1729 | goto fail; | |
1730 | } | |
1731 | ||
1732 | cto.ReadIntervalTimeout = MAXDWORD; | |
1733 | if (!SetCommTimeouts(s->hcom, &cto)) { | |
1734 | fprintf(stderr, "Failed SetCommTimeouts\n"); | |
1735 | goto fail; | |
1736 | } | |
1737 | ||
1738 | if (!ClearCommError(s->hcom, &err, &comstat)) { | |
1739 | fprintf(stderr, "Failed ClearCommError\n"); | |
1740 | goto fail; | |
1741 | } | |
1742 | qemu_add_polling_cb(win_chr_poll, chr); | |
1743 | return 0; | |
1744 | ||
1745 | fail: | |
1746 | win_chr_close(chr); | |
1747 | return -1; | |
1748 | } | |
1749 | ||
1750 | static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) | |
1751 | { | |
1752 | WinCharState *s = chr->opaque; | |
1753 | DWORD len, ret, size, err; | |
1754 | ||
1755 | len = len1; | |
1756 | ZeroMemory(&s->osend, sizeof(s->osend)); | |
1757 | s->osend.hEvent = s->hsend; | |
1758 | while (len > 0) { | |
1759 | if (s->hsend) | |
1760 | ret = WriteFile(s->hcom, buf, len, &size, &s->osend); | |
1761 | else | |
1762 | ret = WriteFile(s->hcom, buf, len, &size, NULL); | |
1763 | if (!ret) { | |
1764 | err = GetLastError(); | |
1765 | if (err == ERROR_IO_PENDING) { | |
1766 | ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); | |
1767 | if (ret) { | |
1768 | buf += size; | |
1769 | len -= size; | |
1770 | } else { | |
1771 | break; | |
1772 | } | |
1773 | } else { | |
1774 | break; | |
1775 | } | |
1776 | } else { | |
1777 | buf += size; | |
1778 | len -= size; | |
1779 | } | |
1780 | } | |
1781 | return len1 - len; | |
1782 | } | |
1783 | ||
1784 | static int win_chr_read_poll(CharDriverState *chr) | |
1785 | { | |
1786 | WinCharState *s = chr->opaque; | |
1787 | ||
909cda12 | 1788 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
1789 | return s->max_size; |
1790 | } | |
1791 | ||
1792 | static void win_chr_readfile(CharDriverState *chr) | |
1793 | { | |
1794 | WinCharState *s = chr->opaque; | |
1795 | int ret, err; | |
9bd7854e | 1796 | uint8_t buf[READ_BUF_LEN]; |
6f97dba0 AL |
1797 | DWORD size; |
1798 | ||
1799 | ZeroMemory(&s->orecv, sizeof(s->orecv)); | |
1800 | s->orecv.hEvent = s->hrecv; | |
1801 | ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); | |
1802 | if (!ret) { | |
1803 | err = GetLastError(); | |
1804 | if (err == ERROR_IO_PENDING) { | |
1805 | ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); | |
1806 | } | |
1807 | } | |
1808 | ||
1809 | if (size > 0) { | |
fa5efccb | 1810 | qemu_chr_be_write(chr, buf, size); |
6f97dba0 AL |
1811 | } |
1812 | } | |
1813 | ||
1814 | static void win_chr_read(CharDriverState *chr) | |
1815 | { | |
1816 | WinCharState *s = chr->opaque; | |
1817 | ||
1818 | if (s->len > s->max_size) | |
1819 | s->len = s->max_size; | |
1820 | if (s->len == 0) | |
1821 | return; | |
1822 | ||
1823 | win_chr_readfile(chr); | |
1824 | } | |
1825 | ||
1826 | static int win_chr_poll(void *opaque) | |
1827 | { | |
1828 | CharDriverState *chr = opaque; | |
1829 | WinCharState *s = chr->opaque; | |
1830 | COMSTAT status; | |
1831 | DWORD comerr; | |
1832 | ||
1833 | ClearCommError(s->hcom, &comerr, &status); | |
1834 | if (status.cbInQue > 0) { | |
1835 | s->len = status.cbInQue; | |
1836 | win_chr_read_poll(chr); | |
1837 | win_chr_read(chr); | |
1838 | return 1; | |
1839 | } | |
1840 | return 0; | |
1841 | } | |
1842 | ||
d59044ef | 1843 | static CharDriverState *qemu_chr_open_win_path(const char *filename) |
6f97dba0 AL |
1844 | { |
1845 | CharDriverState *chr; | |
1846 | WinCharState *s; | |
1847 | ||
7267c094 AL |
1848 | chr = g_malloc0(sizeof(CharDriverState)); |
1849 | s = g_malloc0(sizeof(WinCharState)); | |
6f97dba0 AL |
1850 | chr->opaque = s; |
1851 | chr->chr_write = win_chr_write; | |
1852 | chr->chr_close = win_chr_close; | |
1853 | ||
1854 | if (win_chr_init(chr, filename) < 0) { | |
2e02e18b SW |
1855 | g_free(s); |
1856 | g_free(chr); | |
1f51470d | 1857 | return NULL; |
6f97dba0 | 1858 | } |
fee204fd | 1859 | qemu_chr_be_generic_open(chr); |
1f51470d | 1860 | return chr; |
6f97dba0 AL |
1861 | } |
1862 | ||
1863 | static int win_chr_pipe_poll(void *opaque) | |
1864 | { | |
1865 | CharDriverState *chr = opaque; | |
1866 | WinCharState *s = chr->opaque; | |
1867 | DWORD size; | |
1868 | ||
1869 | PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); | |
1870 | if (size > 0) { | |
1871 | s->len = size; | |
1872 | win_chr_read_poll(chr); | |
1873 | win_chr_read(chr); | |
1874 | return 1; | |
1875 | } | |
1876 | return 0; | |
1877 | } | |
1878 | ||
1879 | static int win_chr_pipe_init(CharDriverState *chr, const char *filename) | |
1880 | { | |
1881 | WinCharState *s = chr->opaque; | |
1882 | OVERLAPPED ov; | |
1883 | int ret; | |
1884 | DWORD size; | |
1885 | char openname[256]; | |
1886 | ||
1887 | s->fpipe = TRUE; | |
1888 | ||
1889 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); | |
1890 | if (!s->hsend) { | |
1891 | fprintf(stderr, "Failed CreateEvent\n"); | |
1892 | goto fail; | |
1893 | } | |
1894 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); | |
1895 | if (!s->hrecv) { | |
1896 | fprintf(stderr, "Failed CreateEvent\n"); | |
1897 | goto fail; | |
1898 | } | |
1899 | ||
1900 | snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename); | |
1901 | s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, | |
1902 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | | |
1903 | PIPE_WAIT, | |
1904 | MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL); | |
1905 | if (s->hcom == INVALID_HANDLE_VALUE) { | |
1906 | fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError()); | |
1907 | s->hcom = NULL; | |
1908 | goto fail; | |
1909 | } | |
1910 | ||
1911 | ZeroMemory(&ov, sizeof(ov)); | |
1912 | ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); | |
1913 | ret = ConnectNamedPipe(s->hcom, &ov); | |
1914 | if (ret) { | |
1915 | fprintf(stderr, "Failed ConnectNamedPipe\n"); | |
1916 | goto fail; | |
1917 | } | |
1918 | ||
1919 | ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); | |
1920 | if (!ret) { | |
1921 | fprintf(stderr, "Failed GetOverlappedResult\n"); | |
1922 | if (ov.hEvent) { | |
1923 | CloseHandle(ov.hEvent); | |
1924 | ov.hEvent = NULL; | |
1925 | } | |
1926 | goto fail; | |
1927 | } | |
1928 | ||
1929 | if (ov.hEvent) { | |
1930 | CloseHandle(ov.hEvent); | |
1931 | ov.hEvent = NULL; | |
1932 | } | |
1933 | qemu_add_polling_cb(win_chr_pipe_poll, chr); | |
1934 | return 0; | |
1935 | ||
1936 | fail: | |
1937 | win_chr_close(chr); | |
1938 | return -1; | |
1939 | } | |
1940 | ||
1941 | ||
548cbb36 | 1942 | static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts) |
6f97dba0 | 1943 | { |
548cbb36 | 1944 | const char *filename = opts->device; |
6f97dba0 AL |
1945 | CharDriverState *chr; |
1946 | WinCharState *s; | |
1947 | ||
7267c094 AL |
1948 | chr = g_malloc0(sizeof(CharDriverState)); |
1949 | s = g_malloc0(sizeof(WinCharState)); | |
6f97dba0 AL |
1950 | chr->opaque = s; |
1951 | chr->chr_write = win_chr_write; | |
1952 | chr->chr_close = win_chr_close; | |
1953 | ||
1954 | if (win_chr_pipe_init(chr, filename) < 0) { | |
2e02e18b SW |
1955 | g_free(s); |
1956 | g_free(chr); | |
1f51470d | 1957 | return NULL; |
6f97dba0 | 1958 | } |
fee204fd | 1959 | qemu_chr_be_generic_open(chr); |
1f51470d | 1960 | return chr; |
6f97dba0 AL |
1961 | } |
1962 | ||
1f51470d | 1963 | static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out) |
6f97dba0 AL |
1964 | { |
1965 | CharDriverState *chr; | |
1966 | WinCharState *s; | |
1967 | ||
7267c094 AL |
1968 | chr = g_malloc0(sizeof(CharDriverState)); |
1969 | s = g_malloc0(sizeof(WinCharState)); | |
6f97dba0 AL |
1970 | s->hcom = fd_out; |
1971 | chr->opaque = s; | |
1972 | chr->chr_write = win_chr_write; | |
fee204fd | 1973 | qemu_chr_be_generic_open(chr); |
1f51470d | 1974 | return chr; |
6f97dba0 AL |
1975 | } |
1976 | ||
d9ac374f | 1977 | static CharDriverState *qemu_chr_open_win_con(void) |
6f97dba0 | 1978 | { |
1f51470d | 1979 | return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE)); |
6f97dba0 AL |
1980 | } |
1981 | ||
db418a0a FC |
1982 | static int win_stdio_write(CharDriverState *chr, const uint8_t *buf, int len) |
1983 | { | |
1984 | HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); | |
1985 | DWORD dwSize; | |
1986 | int len1; | |
1987 | ||
1988 | len1 = len; | |
1989 | ||
1990 | while (len1 > 0) { | |
1991 | if (!WriteFile(hStdOut, buf, len1, &dwSize, NULL)) { | |
1992 | break; | |
1993 | } | |
1994 | buf += dwSize; | |
1995 | len1 -= dwSize; | |
1996 | } | |
1997 | ||
1998 | return len - len1; | |
1999 | } | |
2000 | ||
2001 | static void win_stdio_wait_func(void *opaque) | |
2002 | { | |
2003 | CharDriverState *chr = opaque; | |
2004 | WinStdioCharState *stdio = chr->opaque; | |
2005 | INPUT_RECORD buf[4]; | |
2006 | int ret; | |
2007 | DWORD dwSize; | |
2008 | int i; | |
2009 | ||
2010 | ret = ReadConsoleInput(stdio->hStdIn, buf, sizeof(buf) / sizeof(*buf), | |
2011 | &dwSize); | |
2012 | ||
2013 | if (!ret) { | |
2014 | /* Avoid error storm */ | |
2015 | qemu_del_wait_object(stdio->hStdIn, NULL, NULL); | |
2016 | return; | |
2017 | } | |
2018 | ||
2019 | for (i = 0; i < dwSize; i++) { | |
2020 | KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent; | |
2021 | ||
2022 | if (buf[i].EventType == KEY_EVENT && kev->bKeyDown) { | |
2023 | int j; | |
2024 | if (kev->uChar.AsciiChar != 0) { | |
2025 | for (j = 0; j < kev->wRepeatCount; j++) { | |
2026 | if (qemu_chr_be_can_write(chr)) { | |
2027 | uint8_t c = kev->uChar.AsciiChar; | |
2028 | qemu_chr_be_write(chr, &c, 1); | |
2029 | } | |
2030 | } | |
2031 | } | |
2032 | } | |
2033 | } | |
2034 | } | |
2035 | ||
2036 | static DWORD WINAPI win_stdio_thread(LPVOID param) | |
2037 | { | |
2038 | CharDriverState *chr = param; | |
2039 | WinStdioCharState *stdio = chr->opaque; | |
2040 | int ret; | |
2041 | DWORD dwSize; | |
2042 | ||
2043 | while (1) { | |
2044 | ||
2045 | /* Wait for one byte */ | |
2046 | ret = ReadFile(stdio->hStdIn, &stdio->win_stdio_buf, 1, &dwSize, NULL); | |
2047 | ||
2048 | /* Exit in case of error, continue if nothing read */ | |
2049 | if (!ret) { | |
2050 | break; | |
2051 | } | |
2052 | if (!dwSize) { | |
2053 | continue; | |
2054 | } | |
2055 | ||
2056 | /* Some terminal emulator returns \r\n for Enter, just pass \n */ | |
2057 | if (stdio->win_stdio_buf == '\r') { | |
2058 | continue; | |
2059 | } | |
2060 | ||
2061 | /* Signal the main thread and wait until the byte was eaten */ | |
2062 | if (!SetEvent(stdio->hInputReadyEvent)) { | |
2063 | break; | |
2064 | } | |
2065 | if (WaitForSingleObject(stdio->hInputDoneEvent, INFINITE) | |
2066 | != WAIT_OBJECT_0) { | |
2067 | break; | |
2068 | } | |
2069 | } | |
2070 | ||
2071 | qemu_del_wait_object(stdio->hInputReadyEvent, NULL, NULL); | |
2072 | return 0; | |
2073 | } | |
2074 | ||
2075 | static void win_stdio_thread_wait_func(void *opaque) | |
2076 | { | |
2077 | CharDriverState *chr = opaque; | |
2078 | WinStdioCharState *stdio = chr->opaque; | |
2079 | ||
2080 | if (qemu_chr_be_can_write(chr)) { | |
2081 | qemu_chr_be_write(chr, &stdio->win_stdio_buf, 1); | |
2082 | } | |
2083 | ||
2084 | SetEvent(stdio->hInputDoneEvent); | |
2085 | } | |
2086 | ||
2087 | static void qemu_chr_set_echo_win_stdio(CharDriverState *chr, bool echo) | |
2088 | { | |
2089 | WinStdioCharState *stdio = chr->opaque; | |
2090 | DWORD dwMode = 0; | |
2091 | ||
2092 | GetConsoleMode(stdio->hStdIn, &dwMode); | |
2093 | ||
2094 | if (echo) { | |
2095 | SetConsoleMode(stdio->hStdIn, dwMode | ENABLE_ECHO_INPUT); | |
2096 | } else { | |
2097 | SetConsoleMode(stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT); | |
2098 | } | |
2099 | } | |
2100 | ||
2101 | static void win_stdio_close(CharDriverState *chr) | |
2102 | { | |
2103 | WinStdioCharState *stdio = chr->opaque; | |
2104 | ||
2105 | if (stdio->hInputReadyEvent != INVALID_HANDLE_VALUE) { | |
2106 | CloseHandle(stdio->hInputReadyEvent); | |
2107 | } | |
2108 | if (stdio->hInputDoneEvent != INVALID_HANDLE_VALUE) { | |
2109 | CloseHandle(stdio->hInputDoneEvent); | |
2110 | } | |
2111 | if (stdio->hInputThread != INVALID_HANDLE_VALUE) { | |
2112 | TerminateThread(stdio->hInputThread, 0); | |
2113 | } | |
2114 | ||
2115 | g_free(chr->opaque); | |
2116 | g_free(chr); | |
db418a0a FC |
2117 | } |
2118 | ||
7c358031 | 2119 | static CharDriverState *qemu_chr_open_stdio(ChardevStdio *opts) |
db418a0a FC |
2120 | { |
2121 | CharDriverState *chr; | |
2122 | WinStdioCharState *stdio; | |
2123 | DWORD dwMode; | |
2124 | int is_console = 0; | |
2125 | ||
db418a0a FC |
2126 | chr = g_malloc0(sizeof(CharDriverState)); |
2127 | stdio = g_malloc0(sizeof(WinStdioCharState)); | |
2128 | ||
2129 | stdio->hStdIn = GetStdHandle(STD_INPUT_HANDLE); | |
2130 | if (stdio->hStdIn == INVALID_HANDLE_VALUE) { | |
2131 | fprintf(stderr, "cannot open stdio: invalid handle\n"); | |
2132 | exit(1); | |
2133 | } | |
2134 | ||
2135 | is_console = GetConsoleMode(stdio->hStdIn, &dwMode) != 0; | |
2136 | ||
2137 | chr->opaque = stdio; | |
2138 | chr->chr_write = win_stdio_write; | |
2139 | chr->chr_close = win_stdio_close; | |
2140 | ||
ed7a1540 AL |
2141 | if (is_console) { |
2142 | if (qemu_add_wait_object(stdio->hStdIn, | |
2143 | win_stdio_wait_func, chr)) { | |
2144 | fprintf(stderr, "qemu_add_wait_object: failed\n"); | |
2145 | } | |
2146 | } else { | |
2147 | DWORD dwId; | |
2148 | ||
2149 | stdio->hInputReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL); | |
2150 | stdio->hInputDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL); | |
2151 | stdio->hInputThread = CreateThread(NULL, 0, win_stdio_thread, | |
2152 | chr, 0, &dwId); | |
2153 | ||
2154 | if (stdio->hInputThread == INVALID_HANDLE_VALUE | |
2155 | || stdio->hInputReadyEvent == INVALID_HANDLE_VALUE | |
2156 | || stdio->hInputDoneEvent == INVALID_HANDLE_VALUE) { | |
2157 | fprintf(stderr, "cannot create stdio thread or event\n"); | |
2158 | exit(1); | |
2159 | } | |
2160 | if (qemu_add_wait_object(stdio->hInputReadyEvent, | |
2161 | win_stdio_thread_wait_func, chr)) { | |
2162 | fprintf(stderr, "qemu_add_wait_object: failed\n"); | |
db418a0a FC |
2163 | } |
2164 | } | |
2165 | ||
2166 | dwMode |= ENABLE_LINE_INPUT; | |
2167 | ||
ed7a1540 | 2168 | if (is_console) { |
db418a0a FC |
2169 | /* set the terminal in raw mode */ |
2170 | /* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */ | |
2171 | dwMode |= ENABLE_PROCESSED_INPUT; | |
2172 | } | |
2173 | ||
2174 | SetConsoleMode(stdio->hStdIn, dwMode); | |
2175 | ||
2176 | chr->chr_set_echo = qemu_chr_set_echo_win_stdio; | |
2177 | qemu_chr_fe_set_echo(chr, false); | |
2178 | ||
1f51470d | 2179 | return chr; |
db418a0a | 2180 | } |
6f97dba0 AL |
2181 | #endif /* !_WIN32 */ |
2182 | ||
5ab8211b | 2183 | |
6f97dba0 AL |
2184 | /***********************************************************/ |
2185 | /* UDP Net console */ | |
2186 | ||
2187 | typedef struct { | |
2188 | int fd; | |
76a9644b AL |
2189 | GIOChannel *chan; |
2190 | guint tag; | |
9bd7854e | 2191 | uint8_t buf[READ_BUF_LEN]; |
6f97dba0 AL |
2192 | int bufcnt; |
2193 | int bufptr; | |
2194 | int max_size; | |
2195 | } NetCharDriver; | |
2196 | ||
2197 | static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) | |
2198 | { | |
2199 | NetCharDriver *s = chr->opaque; | |
76a9644b AL |
2200 | gsize bytes_written; |
2201 | GIOStatus status; | |
2202 | ||
2203 | status = g_io_channel_write_chars(s->chan, (const gchar *)buf, len, &bytes_written, NULL); | |
2204 | if (status == G_IO_STATUS_EOF) { | |
2205 | return 0; | |
2206 | } else if (status != G_IO_STATUS_NORMAL) { | |
2207 | return -1; | |
2208 | } | |
6f97dba0 | 2209 | |
76a9644b | 2210 | return bytes_written; |
6f97dba0 AL |
2211 | } |
2212 | ||
2213 | static int udp_chr_read_poll(void *opaque) | |
2214 | { | |
2215 | CharDriverState *chr = opaque; | |
2216 | NetCharDriver *s = chr->opaque; | |
2217 | ||
909cda12 | 2218 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
2219 | |
2220 | /* If there were any stray characters in the queue process them | |
2221 | * first | |
2222 | */ | |
2223 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { | |
fa5efccb | 2224 | qemu_chr_be_write(chr, &s->buf[s->bufptr], 1); |
6f97dba0 | 2225 | s->bufptr++; |
909cda12 | 2226 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
2227 | } |
2228 | return s->max_size; | |
2229 | } | |
2230 | ||
76a9644b | 2231 | static gboolean udp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) |
6f97dba0 AL |
2232 | { |
2233 | CharDriverState *chr = opaque; | |
2234 | NetCharDriver *s = chr->opaque; | |
76a9644b AL |
2235 | gsize bytes_read = 0; |
2236 | GIOStatus status; | |
6f97dba0 AL |
2237 | |
2238 | if (s->max_size == 0) | |
76a9644b AL |
2239 | return FALSE; |
2240 | status = g_io_channel_read_chars(s->chan, (gchar *)s->buf, sizeof(s->buf), | |
2241 | &bytes_read, NULL); | |
2242 | s->bufcnt = bytes_read; | |
6f97dba0 | 2243 | s->bufptr = s->bufcnt; |
76a9644b AL |
2244 | if (status != G_IO_STATUS_NORMAL) { |
2245 | return FALSE; | |
2246 | } | |
6f97dba0 AL |
2247 | |
2248 | s->bufptr = 0; | |
2249 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { | |
fa5efccb | 2250 | qemu_chr_be_write(chr, &s->buf[s->bufptr], 1); |
6f97dba0 | 2251 | s->bufptr++; |
909cda12 | 2252 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 | 2253 | } |
76a9644b AL |
2254 | |
2255 | return TRUE; | |
6f97dba0 AL |
2256 | } |
2257 | ||
2258 | static void udp_chr_update_read_handler(CharDriverState *chr) | |
2259 | { | |
2260 | NetCharDriver *s = chr->opaque; | |
2261 | ||
76a9644b AL |
2262 | if (s->tag) { |
2263 | g_source_remove(s->tag); | |
2264 | s->tag = 0; | |
2265 | } | |
2266 | ||
2267 | if (s->chan) { | |
2268 | s->tag = io_add_watch_poll(s->chan, udp_chr_read_poll, udp_chr_read, chr); | |
6f97dba0 AL |
2269 | } |
2270 | } | |
2271 | ||
819f56b7 AL |
2272 | static void udp_chr_close(CharDriverState *chr) |
2273 | { | |
2274 | NetCharDriver *s = chr->opaque; | |
76a9644b AL |
2275 | if (s->tag) { |
2276 | g_source_remove(s->tag); | |
2277 | } | |
2278 | if (s->chan) { | |
2279 | g_io_channel_unref(s->chan); | |
819f56b7 AL |
2280 | closesocket(s->fd); |
2281 | } | |
7267c094 | 2282 | g_free(s); |
a425d23f | 2283 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
819f56b7 AL |
2284 | } |
2285 | ||
3ecc059d | 2286 | static CharDriverState *qemu_chr_open_udp_fd(int fd) |
6f97dba0 AL |
2287 | { |
2288 | CharDriverState *chr = NULL; | |
2289 | NetCharDriver *s = NULL; | |
6f97dba0 | 2290 | |
7267c094 AL |
2291 | chr = g_malloc0(sizeof(CharDriverState)); |
2292 | s = g_malloc0(sizeof(NetCharDriver)); | |
6f97dba0 | 2293 | |
6f97dba0 | 2294 | s->fd = fd; |
76a9644b | 2295 | s->chan = io_channel_from_socket(s->fd); |
6f97dba0 AL |
2296 | s->bufcnt = 0; |
2297 | s->bufptr = 0; | |
2298 | chr->opaque = s; | |
2299 | chr->chr_write = udp_chr_write; | |
2300 | chr->chr_update_read_handler = udp_chr_update_read_handler; | |
819f56b7 | 2301 | chr->chr_close = udp_chr_close; |
1f51470d | 2302 | return chr; |
3ecc059d | 2303 | } |
6f97dba0 | 2304 | |
3ecc059d GH |
2305 | static CharDriverState *qemu_chr_open_udp(QemuOpts *opts) |
2306 | { | |
2307 | Error *local_err = NULL; | |
2308 | int fd = -1; | |
2309 | ||
2310 | fd = inet_dgram_opts(opts, &local_err); | |
2311 | if (fd < 0) { | |
2312 | return NULL; | |
6e1db57b | 2313 | } |
3ecc059d | 2314 | return qemu_chr_open_udp_fd(fd); |
6f97dba0 AL |
2315 | } |
2316 | ||
2317 | /***********************************************************/ | |
2318 | /* TCP Net console */ | |
2319 | ||
2320 | typedef struct { | |
2ea5a7af AL |
2321 | |
2322 | GIOChannel *chan, *listen_chan; | |
2323 | guint tag, listen_tag; | |
6f97dba0 AL |
2324 | int fd, listen_fd; |
2325 | int connected; | |
2326 | int max_size; | |
2327 | int do_telnetopt; | |
2328 | int do_nodelay; | |
2329 | int is_unix; | |
7d174059 | 2330 | int msgfd; |
6f97dba0 AL |
2331 | } TCPCharDriver; |
2332 | ||
2ea5a7af | 2333 | static gboolean tcp_chr_accept(GIOChannel *chan, GIOCondition cond, void *opaque); |
6f97dba0 AL |
2334 | |
2335 | static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) | |
2336 | { | |
2337 | TCPCharDriver *s = chr->opaque; | |
2338 | if (s->connected) { | |
684a096e | 2339 | return io_channel_send(s->chan, buf, len); |
455aa1e0 | 2340 | } else { |
6db0fdce | 2341 | /* XXX: indicate an error ? */ |
455aa1e0 | 2342 | return len; |
6f97dba0 AL |
2343 | } |
2344 | } | |
2345 | ||
2346 | static int tcp_chr_read_poll(void *opaque) | |
2347 | { | |
2348 | CharDriverState *chr = opaque; | |
2349 | TCPCharDriver *s = chr->opaque; | |
2350 | if (!s->connected) | |
2351 | return 0; | |
909cda12 | 2352 | s->max_size = qemu_chr_be_can_write(chr); |
6f97dba0 AL |
2353 | return s->max_size; |
2354 | } | |
2355 | ||
2356 | #define IAC 255 | |
2357 | #define IAC_BREAK 243 | |
2358 | static void tcp_chr_process_IAC_bytes(CharDriverState *chr, | |
2359 | TCPCharDriver *s, | |
2360 | uint8_t *buf, int *size) | |
2361 | { | |
2362 | /* Handle any telnet client's basic IAC options to satisfy char by | |
2363 | * char mode with no echo. All IAC options will be removed from | |
2364 | * the buf and the do_telnetopt variable will be used to track the | |
2365 | * state of the width of the IAC information. | |
2366 | * | |
2367 | * IAC commands come in sets of 3 bytes with the exception of the | |
2368 | * "IAC BREAK" command and the double IAC. | |
2369 | */ | |
2370 | ||
2371 | int i; | |
2372 | int j = 0; | |
2373 | ||
2374 | for (i = 0; i < *size; i++) { | |
2375 | if (s->do_telnetopt > 1) { | |
2376 | if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { | |
2377 | /* Double IAC means send an IAC */ | |
2378 | if (j != i) | |
2379 | buf[j] = buf[i]; | |
2380 | j++; | |
2381 | s->do_telnetopt = 1; | |
2382 | } else { | |
2383 | if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { | |
2384 | /* Handle IAC break commands by sending a serial break */ | |
a425d23f | 2385 | qemu_chr_be_event(chr, CHR_EVENT_BREAK); |
6f97dba0 AL |
2386 | s->do_telnetopt++; |
2387 | } | |
2388 | s->do_telnetopt++; | |
2389 | } | |
2390 | if (s->do_telnetopt >= 4) { | |
2391 | s->do_telnetopt = 1; | |
2392 | } | |
2393 | } else { | |
2394 | if ((unsigned char)buf[i] == IAC) { | |
2395 | s->do_telnetopt = 2; | |
2396 | } else { | |
2397 | if (j != i) | |
2398 | buf[j] = buf[i]; | |
2399 | j++; | |
2400 | } | |
2401 | } | |
2402 | } | |
2403 | *size = j; | |
2404 | } | |
2405 | ||
7d174059 MM |
2406 | static int tcp_get_msgfd(CharDriverState *chr) |
2407 | { | |
2408 | TCPCharDriver *s = chr->opaque; | |
e53f27b9 PB |
2409 | int fd = s->msgfd; |
2410 | s->msgfd = -1; | |
2411 | return fd; | |
7d174059 MM |
2412 | } |
2413 | ||
73bcc2ac | 2414 | #ifndef _WIN32 |
7d174059 MM |
2415 | static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg) |
2416 | { | |
2417 | TCPCharDriver *s = chr->opaque; | |
2418 | struct cmsghdr *cmsg; | |
2419 | ||
2420 | for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { | |
2421 | int fd; | |
2422 | ||
2423 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) || | |
2424 | cmsg->cmsg_level != SOL_SOCKET || | |
2425 | cmsg->cmsg_type != SCM_RIGHTS) | |
2426 | continue; | |
2427 | ||
2428 | fd = *((int *)CMSG_DATA(cmsg)); | |
2429 | if (fd < 0) | |
2430 | continue; | |
2431 | ||
9b938c72 SH |
2432 | /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */ |
2433 | qemu_set_block(fd); | |
2434 | ||
06138651 CB |
2435 | #ifndef MSG_CMSG_CLOEXEC |
2436 | qemu_set_cloexec(fd); | |
2437 | #endif | |
7d174059 MM |
2438 | if (s->msgfd != -1) |
2439 | close(s->msgfd); | |
2440 | s->msgfd = fd; | |
2441 | } | |
2442 | } | |
2443 | ||
9977c894 MM |
2444 | static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len) |
2445 | { | |
2446 | TCPCharDriver *s = chr->opaque; | |
7cba04f6 | 2447 | struct msghdr msg = { NULL, }; |
9977c894 | 2448 | struct iovec iov[1]; |
7d174059 MM |
2449 | union { |
2450 | struct cmsghdr cmsg; | |
2451 | char control[CMSG_SPACE(sizeof(int))]; | |
2452 | } msg_control; | |
06138651 | 2453 | int flags = 0; |
7d174059 | 2454 | ssize_t ret; |
9977c894 MM |
2455 | |
2456 | iov[0].iov_base = buf; | |
2457 | iov[0].iov_len = len; | |
2458 | ||
2459 | msg.msg_iov = iov; | |
2460 | msg.msg_iovlen = 1; | |
7d174059 MM |
2461 | msg.msg_control = &msg_control; |
2462 | msg.msg_controllen = sizeof(msg_control); | |
2463 | ||
06138651 CB |
2464 | #ifdef MSG_CMSG_CLOEXEC |
2465 | flags |= MSG_CMSG_CLOEXEC; | |
2466 | #endif | |
2467 | ret = recvmsg(s->fd, &msg, flags); | |
2468 | if (ret > 0 && s->is_unix) { | |
7d174059 | 2469 | unix_process_msgfd(chr, &msg); |
06138651 | 2470 | } |
9977c894 | 2471 | |
7d174059 | 2472 | return ret; |
9977c894 MM |
2473 | } |
2474 | #else | |
2475 | static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len) | |
2476 | { | |
2477 | TCPCharDriver *s = chr->opaque; | |
00aa0040 | 2478 | return qemu_recv(s->fd, buf, len, 0); |
9977c894 MM |
2479 | } |
2480 | #endif | |
2481 | ||
d3cc5bc4 AS |
2482 | static GSource *tcp_chr_add_watch(CharDriverState *chr, GIOCondition cond) |
2483 | { | |
2484 | TCPCharDriver *s = chr->opaque; | |
2485 | return g_io_create_watch(s->chan, cond); | |
2486 | } | |
2487 | ||
2ea5a7af | 2488 | static gboolean tcp_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque) |
6f97dba0 AL |
2489 | { |
2490 | CharDriverState *chr = opaque; | |
2491 | TCPCharDriver *s = chr->opaque; | |
9bd7854e | 2492 | uint8_t buf[READ_BUF_LEN]; |
6f97dba0 AL |
2493 | int len, size; |
2494 | ||
2ea5a7af AL |
2495 | if (!s->connected || s->max_size <= 0) { |
2496 | return FALSE; | |
2497 | } | |
6f97dba0 AL |
2498 | len = sizeof(buf); |
2499 | if (len > s->max_size) | |
2500 | len = s->max_size; | |
9977c894 | 2501 | size = tcp_chr_recv(chr, (void *)buf, len); |
6f97dba0 AL |
2502 | if (size == 0) { |
2503 | /* connection closed */ | |
2504 | s->connected = 0; | |
2ea5a7af AL |
2505 | if (s->listen_chan) { |
2506 | s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr); | |
6f97dba0 | 2507 | } |
2ea5a7af AL |
2508 | g_source_remove(s->tag); |
2509 | s->tag = 0; | |
2510 | g_io_channel_unref(s->chan); | |
2511 | s->chan = NULL; | |
6f97dba0 AL |
2512 | closesocket(s->fd); |
2513 | s->fd = -1; | |
a425d23f | 2514 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
2515 | } else if (size > 0) { |
2516 | if (s->do_telnetopt) | |
2517 | tcp_chr_process_IAC_bytes(chr, s, buf, &size); | |
2518 | if (size > 0) | |
fa5efccb | 2519 | qemu_chr_be_write(chr, buf, size); |
6f97dba0 | 2520 | } |
2ea5a7af AL |
2521 | |
2522 | return TRUE; | |
6f97dba0 AL |
2523 | } |
2524 | ||
68c18d1c BS |
2525 | #ifndef _WIN32 |
2526 | CharDriverState *qemu_chr_open_eventfd(int eventfd) | |
2527 | { | |
6cbf4c8c | 2528 | return qemu_chr_open_fd(eventfd, eventfd); |
6cbf4c8c | 2529 | } |
68c18d1c | 2530 | #endif |
6cbf4c8c | 2531 | |
6f97dba0 AL |
2532 | static void tcp_chr_connect(void *opaque) |
2533 | { | |
2534 | CharDriverState *chr = opaque; | |
2535 | TCPCharDriver *s = chr->opaque; | |
2536 | ||
2537 | s->connected = 1; | |
2ea5a7af AL |
2538 | if (s->chan) { |
2539 | s->tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, tcp_chr_read, chr); | |
bbdd2ad0 | 2540 | } |
fee204fd | 2541 | qemu_chr_be_generic_open(chr); |
6f97dba0 AL |
2542 | } |
2543 | ||
2544 | #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; | |
2545 | static void tcp_chr_telnet_init(int fd) | |
2546 | { | |
2547 | char buf[3]; | |
2548 | /* Send the telnet negotion to put telnet in binary, no echo, single char mode */ | |
2549 | IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ | |
2550 | send(fd, (char *)buf, 3, 0); | |
2551 | IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ | |
2552 | send(fd, (char *)buf, 3, 0); | |
2553 | IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ | |
2554 | send(fd, (char *)buf, 3, 0); | |
2555 | IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ | |
2556 | send(fd, (char *)buf, 3, 0); | |
2557 | } | |
2558 | ||
13661089 DB |
2559 | static int tcp_chr_add_client(CharDriverState *chr, int fd) |
2560 | { | |
2561 | TCPCharDriver *s = chr->opaque; | |
2562 | if (s->fd != -1) | |
2563 | return -1; | |
2564 | ||
f9e8cacc | 2565 | qemu_set_nonblock(fd); |
13661089 DB |
2566 | if (s->do_nodelay) |
2567 | socket_set_nodelay(fd); | |
2568 | s->fd = fd; | |
2ea5a7af AL |
2569 | s->chan = io_channel_from_socket(fd); |
2570 | g_source_remove(s->listen_tag); | |
2571 | s->listen_tag = 0; | |
13661089 DB |
2572 | tcp_chr_connect(chr); |
2573 | ||
2574 | return 0; | |
2575 | } | |
2576 | ||
2ea5a7af | 2577 | static gboolean tcp_chr_accept(GIOChannel *channel, GIOCondition cond, void *opaque) |
6f97dba0 AL |
2578 | { |
2579 | CharDriverState *chr = opaque; | |
2580 | TCPCharDriver *s = chr->opaque; | |
2581 | struct sockaddr_in saddr; | |
2582 | #ifndef _WIN32 | |
2583 | struct sockaddr_un uaddr; | |
2584 | #endif | |
2585 | struct sockaddr *addr; | |
2586 | socklen_t len; | |
2587 | int fd; | |
2588 | ||
2589 | for(;;) { | |
2590 | #ifndef _WIN32 | |
2591 | if (s->is_unix) { | |
2592 | len = sizeof(uaddr); | |
2593 | addr = (struct sockaddr *)&uaddr; | |
2594 | } else | |
2595 | #endif | |
2596 | { | |
2597 | len = sizeof(saddr); | |
2598 | addr = (struct sockaddr *)&saddr; | |
2599 | } | |
40ff6d7e | 2600 | fd = qemu_accept(s->listen_fd, addr, &len); |
6f97dba0 | 2601 | if (fd < 0 && errno != EINTR) { |
2ea5a7af | 2602 | return FALSE; |
6f97dba0 AL |
2603 | } else if (fd >= 0) { |
2604 | if (s->do_telnetopt) | |
2605 | tcp_chr_telnet_init(fd); | |
2606 | break; | |
2607 | } | |
2608 | } | |
13661089 DB |
2609 | if (tcp_chr_add_client(chr, fd) < 0) |
2610 | close(fd); | |
2ea5a7af AL |
2611 | |
2612 | return TRUE; | |
6f97dba0 AL |
2613 | } |
2614 | ||
2615 | static void tcp_chr_close(CharDriverState *chr) | |
2616 | { | |
2617 | TCPCharDriver *s = chr->opaque; | |
819f56b7 | 2618 | if (s->fd >= 0) { |
2ea5a7af AL |
2619 | if (s->tag) { |
2620 | g_source_remove(s->tag); | |
2621 | } | |
2622 | if (s->chan) { | |
2623 | g_io_channel_unref(s->chan); | |
2624 | } | |
6f97dba0 | 2625 | closesocket(s->fd); |
819f56b7 AL |
2626 | } |
2627 | if (s->listen_fd >= 0) { | |
2ea5a7af AL |
2628 | if (s->listen_tag) { |
2629 | g_source_remove(s->listen_tag); | |
2630 | } | |
2631 | if (s->listen_chan) { | |
2632 | g_io_channel_unref(s->listen_chan); | |
2633 | } | |
6f97dba0 | 2634 | closesocket(s->listen_fd); |
819f56b7 | 2635 | } |
7267c094 | 2636 | g_free(s); |
a425d23f | 2637 | qemu_chr_be_event(chr, CHR_EVENT_CLOSED); |
6f97dba0 AL |
2638 | } |
2639 | ||
f6bd5d6e GH |
2640 | static CharDriverState *qemu_chr_open_socket_fd(int fd, bool do_nodelay, |
2641 | bool is_listen, bool is_telnet, | |
2642 | bool is_waitconnect, | |
2643 | Error **errp) | |
6f97dba0 AL |
2644 | { |
2645 | CharDriverState *chr = NULL; | |
2646 | TCPCharDriver *s = NULL; | |
f6bd5d6e GH |
2647 | char host[NI_MAXHOST], serv[NI_MAXSERV]; |
2648 | const char *left = "", *right = ""; | |
2649 | struct sockaddr_storage ss; | |
2650 | socklen_t ss_len = sizeof(ss); | |
2651 | ||
2652 | memset(&ss, 0, ss_len); | |
2653 | if (getsockname(fd, (struct sockaddr *) &ss, &ss_len) != 0) { | |
2654 | error_setg(errp, "getsockname: %s", strerror(errno)); | |
2655 | return NULL; | |
2656 | } | |
2657 | ||
2658 | chr = g_malloc0(sizeof(CharDriverState)); | |
2659 | s = g_malloc0(sizeof(TCPCharDriver)); | |
2660 | ||
2661 | s->connected = 0; | |
2662 | s->fd = -1; | |
2663 | s->listen_fd = -1; | |
2664 | s->msgfd = -1; | |
2665 | ||
2666 | chr->filename = g_malloc(256); | |
2667 | switch (ss.ss_family) { | |
2668 | #ifndef _WIN32 | |
2669 | case AF_UNIX: | |
2670 | s->is_unix = 1; | |
2671 | snprintf(chr->filename, 256, "unix:%s%s", | |
2672 | ((struct sockaddr_un *)(&ss))->sun_path, | |
2673 | is_listen ? ",server" : ""); | |
2674 | break; | |
2675 | #endif | |
2676 | case AF_INET6: | |
2677 | left = "["; | |
2678 | right = "]"; | |
2679 | /* fall through */ | |
2680 | case AF_INET: | |
2681 | s->do_nodelay = do_nodelay; | |
2682 | getnameinfo((struct sockaddr *) &ss, ss_len, host, sizeof(host), | |
2683 | serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV); | |
e5545854 | 2684 | snprintf(chr->filename, 256, "%s:%s%s%s:%s%s", |
f6bd5d6e GH |
2685 | is_telnet ? "telnet" : "tcp", |
2686 | left, host, right, serv, | |
2687 | is_listen ? ",server" : ""); | |
2688 | break; | |
2689 | } | |
2690 | ||
2691 | chr->opaque = s; | |
2692 | chr->chr_write = tcp_chr_write; | |
2693 | chr->chr_close = tcp_chr_close; | |
2694 | chr->get_msgfd = tcp_get_msgfd; | |
2695 | chr->chr_add_client = tcp_chr_add_client; | |
d3cc5bc4 | 2696 | chr->chr_add_watch = tcp_chr_add_watch; |
f6bd5d6e GH |
2697 | |
2698 | if (is_listen) { | |
2699 | s->listen_fd = fd; | |
2ea5a7af AL |
2700 | s->listen_chan = io_channel_from_socket(s->listen_fd); |
2701 | s->listen_tag = g_io_add_watch(s->listen_chan, G_IO_IN, tcp_chr_accept, chr); | |
f6bd5d6e GH |
2702 | if (is_telnet) { |
2703 | s->do_telnetopt = 1; | |
2704 | } | |
2705 | } else { | |
2706 | s->connected = 1; | |
2707 | s->fd = fd; | |
2708 | socket_set_nodelay(fd); | |
2ea5a7af | 2709 | s->chan = io_channel_from_socket(s->fd); |
f6bd5d6e GH |
2710 | tcp_chr_connect(chr); |
2711 | } | |
2712 | ||
2713 | if (is_listen && is_waitconnect) { | |
2714 | printf("QEMU waiting for connection on: %s\n", | |
2715 | chr->filename); | |
2ea5a7af | 2716 | tcp_chr_accept(s->listen_chan, G_IO_IN, chr); |
f9e8cacc | 2717 | qemu_set_nonblock(s->listen_fd); |
f6bd5d6e GH |
2718 | } |
2719 | return chr; | |
2720 | } | |
2721 | ||
2722 | static CharDriverState *qemu_chr_open_socket(QemuOpts *opts) | |
2723 | { | |
2724 | CharDriverState *chr = NULL; | |
87d5f24f | 2725 | Error *local_err = NULL; |
aeb2c47a GH |
2726 | int fd = -1; |
2727 | int is_listen; | |
2728 | int is_waitconnect; | |
2729 | int do_nodelay; | |
2730 | int is_unix; | |
2731 | int is_telnet; | |
2732 | ||
2733 | is_listen = qemu_opt_get_bool(opts, "server", 0); | |
2734 | is_waitconnect = qemu_opt_get_bool(opts, "wait", 1); | |
2735 | is_telnet = qemu_opt_get_bool(opts, "telnet", 0); | |
2736 | do_nodelay = !qemu_opt_get_bool(opts, "delay", 1); | |
2737 | is_unix = qemu_opt_get(opts, "path") != NULL; | |
6f97dba0 AL |
2738 | if (!is_listen) |
2739 | is_waitconnect = 0; | |
2740 | ||
f07b6003 AL |
2741 | if (is_unix) { |
2742 | if (is_listen) { | |
87d5f24f | 2743 | fd = unix_listen_opts(opts, &local_err); |
f07b6003 | 2744 | } else { |
87d5f24f | 2745 | fd = unix_connect_opts(opts, &local_err, NULL, NULL); |
f07b6003 AL |
2746 | } |
2747 | } else { | |
2748 | if (is_listen) { | |
87d5f24f | 2749 | fd = inet_listen_opts(opts, 0, &local_err); |
f07b6003 | 2750 | } else { |
87d5f24f | 2751 | fd = inet_connect_opts(opts, &local_err, NULL, NULL); |
f07b6003 AL |
2752 | } |
2753 | } | |
a89dd6c3 | 2754 | if (fd < 0) { |
6f97dba0 | 2755 | goto fail; |
a89dd6c3 | 2756 | } |
6f97dba0 AL |
2757 | |
2758 | if (!is_waitconnect) | |
f9e8cacc | 2759 | qemu_set_nonblock(fd); |
6f97dba0 | 2760 | |
f6bd5d6e GH |
2761 | chr = qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, is_telnet, |
2762 | is_waitconnect, &local_err); | |
2763 | if (error_is_set(&local_err)) { | |
2764 | goto fail; | |
6f97dba0 | 2765 | } |
1f51470d | 2766 | return chr; |
aeb2c47a | 2767 | |
f6bd5d6e | 2768 | |
6f97dba0 | 2769 | fail: |
87d5f24f PB |
2770 | if (local_err) { |
2771 | qerror_report_err(local_err); | |
2772 | error_free(local_err); | |
2773 | } | |
2774 | if (fd >= 0) { | |
6f97dba0 | 2775 | closesocket(fd); |
87d5f24f | 2776 | } |
f6bd5d6e GH |
2777 | if (chr) { |
2778 | g_free(chr->opaque); | |
2779 | g_free(chr); | |
2780 | } | |
1f51470d | 2781 | return NULL; |
6f97dba0 AL |
2782 | } |
2783 | ||
51767e7c | 2784 | /*********************************************************/ |
3949e594 | 2785 | /* Ring buffer chardev */ |
51767e7c LL |
2786 | |
2787 | typedef struct { | |
2788 | size_t size; | |
2789 | size_t prod; | |
2790 | size_t cons; | |
2791 | uint8_t *cbuf; | |
3949e594 | 2792 | } RingBufCharDriver; |
51767e7c | 2793 | |
3949e594 | 2794 | static size_t ringbuf_count(const CharDriverState *chr) |
51767e7c | 2795 | { |
3949e594 | 2796 | const RingBufCharDriver *d = chr->opaque; |
51767e7c | 2797 | |
5c230105 | 2798 | return d->prod - d->cons; |
51767e7c LL |
2799 | } |
2800 | ||
3949e594 | 2801 | static int ringbuf_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
51767e7c | 2802 | { |
3949e594 | 2803 | RingBufCharDriver *d = chr->opaque; |
51767e7c LL |
2804 | int i; |
2805 | ||
2806 | if (!buf || (len < 0)) { | |
2807 | return -1; | |
2808 | } | |
2809 | ||
2810 | for (i = 0; i < len; i++ ) { | |
5c230105 MA |
2811 | d->cbuf[d->prod++ & (d->size - 1)] = buf[i]; |
2812 | if (d->prod - d->cons > d->size) { | |
51767e7c LL |
2813 | d->cons = d->prod - d->size; |
2814 | } | |
2815 | } | |
2816 | ||
2817 | return 0; | |
2818 | } | |
2819 | ||
3949e594 | 2820 | static int ringbuf_chr_read(CharDriverState *chr, uint8_t *buf, int len) |
51767e7c | 2821 | { |
3949e594 | 2822 | RingBufCharDriver *d = chr->opaque; |
51767e7c LL |
2823 | int i; |
2824 | ||
5c230105 MA |
2825 | for (i = 0; i < len && d->cons != d->prod; i++) { |
2826 | buf[i] = d->cbuf[d->cons++ & (d->size - 1)]; | |
51767e7c LL |
2827 | } |
2828 | ||
2829 | return i; | |
2830 | } | |
2831 | ||
3949e594 | 2832 | static void ringbuf_chr_close(struct CharDriverState *chr) |
51767e7c | 2833 | { |
3949e594 | 2834 | RingBufCharDriver *d = chr->opaque; |
51767e7c LL |
2835 | |
2836 | g_free(d->cbuf); | |
2837 | g_free(d); | |
2838 | chr->opaque = NULL; | |
2839 | } | |
2840 | ||
1da48c65 GH |
2841 | static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts, |
2842 | Error **errp) | |
51767e7c LL |
2843 | { |
2844 | CharDriverState *chr; | |
3949e594 | 2845 | RingBufCharDriver *d; |
51767e7c LL |
2846 | |
2847 | chr = g_malloc0(sizeof(CharDriverState)); | |
2848 | d = g_malloc(sizeof(*d)); | |
2849 | ||
1da48c65 | 2850 | d->size = opts->has_size ? opts->size : 65536; |
51767e7c LL |
2851 | |
2852 | /* The size must be power of 2 */ | |
2853 | if (d->size & (d->size - 1)) { | |
1da48c65 | 2854 | error_setg(errp, "size of ringbuf chardev must be power of two"); |
51767e7c LL |
2855 | goto fail; |
2856 | } | |
2857 | ||
2858 | d->prod = 0; | |
2859 | d->cons = 0; | |
2860 | d->cbuf = g_malloc0(d->size); | |
2861 | ||
2862 | chr->opaque = d; | |
3949e594 MA |
2863 | chr->chr_write = ringbuf_chr_write; |
2864 | chr->chr_close = ringbuf_chr_close; | |
51767e7c LL |
2865 | |
2866 | return chr; | |
2867 | ||
2868 | fail: | |
2869 | g_free(d); | |
2870 | g_free(chr); | |
2871 | return NULL; | |
2872 | } | |
2873 | ||
3949e594 | 2874 | static bool chr_is_ringbuf(const CharDriverState *chr) |
1f590cf9 | 2875 | { |
3949e594 | 2876 | return chr->chr_write == ringbuf_chr_write; |
1f590cf9 LL |
2877 | } |
2878 | ||
3949e594 | 2879 | void qmp_ringbuf_write(const char *device, const char *data, |
82e59a67 | 2880 | bool has_format, enum DataFormat format, |
1f590cf9 LL |
2881 | Error **errp) |
2882 | { | |
2883 | CharDriverState *chr; | |
c4f331b6 | 2884 | const uint8_t *write_data; |
1f590cf9 | 2885 | int ret; |
c4f331b6 | 2886 | size_t write_count; |
1f590cf9 LL |
2887 | |
2888 | chr = qemu_chr_find(device); | |
2889 | if (!chr) { | |
1a69278e | 2890 | error_setg(errp, "Device '%s' not found", device); |
1f590cf9 LL |
2891 | return; |
2892 | } | |
2893 | ||
3949e594 MA |
2894 | if (!chr_is_ringbuf(chr)) { |
2895 | error_setg(errp,"%s is not a ringbuf device", device); | |
1f590cf9 LL |
2896 | return; |
2897 | } | |
2898 | ||
1f590cf9 LL |
2899 | if (has_format && (format == DATA_FORMAT_BASE64)) { |
2900 | write_data = g_base64_decode(data, &write_count); | |
2901 | } else { | |
2902 | write_data = (uint8_t *)data; | |
82e59a67 | 2903 | write_count = strlen(data); |
1f590cf9 LL |
2904 | } |
2905 | ||
3949e594 | 2906 | ret = ringbuf_chr_write(chr, write_data, write_count); |
1f590cf9 | 2907 | |
13289fb5 MA |
2908 | if (write_data != (uint8_t *)data) { |
2909 | g_free((void *)write_data); | |
2910 | } | |
2911 | ||
1f590cf9 LL |
2912 | if (ret < 0) { |
2913 | error_setg(errp, "Failed to write to device %s", device); | |
2914 | return; | |
2915 | } | |
2916 | } | |
2917 | ||
3949e594 | 2918 | char *qmp_ringbuf_read(const char *device, int64_t size, |
3ab651fc MA |
2919 | bool has_format, enum DataFormat format, |
2920 | Error **errp) | |
49b6d722 LL |
2921 | { |
2922 | CharDriverState *chr; | |
c4f331b6 | 2923 | uint8_t *read_data; |
49b6d722 | 2924 | size_t count; |
3ab651fc | 2925 | char *data; |
49b6d722 LL |
2926 | |
2927 | chr = qemu_chr_find(device); | |
2928 | if (!chr) { | |
1a69278e | 2929 | error_setg(errp, "Device '%s' not found", device); |
49b6d722 LL |
2930 | return NULL; |
2931 | } | |
2932 | ||
3949e594 MA |
2933 | if (!chr_is_ringbuf(chr)) { |
2934 | error_setg(errp,"%s is not a ringbuf device", device); | |
49b6d722 LL |
2935 | return NULL; |
2936 | } | |
2937 | ||
2938 | if (size <= 0) { | |
2939 | error_setg(errp, "size must be greater than zero"); | |
2940 | return NULL; | |
2941 | } | |
2942 | ||
3949e594 | 2943 | count = ringbuf_count(chr); |
49b6d722 | 2944 | size = size > count ? count : size; |
44f3bcd2 | 2945 | read_data = g_malloc(size + 1); |
49b6d722 | 2946 | |
3949e594 | 2947 | ringbuf_chr_read(chr, read_data, size); |
49b6d722 LL |
2948 | |
2949 | if (has_format && (format == DATA_FORMAT_BASE64)) { | |
3ab651fc | 2950 | data = g_base64_encode(read_data, size); |
13289fb5 | 2951 | g_free(read_data); |
49b6d722 | 2952 | } else { |
3949e594 MA |
2953 | /* |
2954 | * FIXME should read only complete, valid UTF-8 characters up | |
2955 | * to @size bytes. Invalid sequences should be replaced by a | |
2956 | * suitable replacement character. Except when (and only | |
2957 | * when) ring buffer lost characters since last read, initial | |
2958 | * continuation characters should be dropped. | |
2959 | */ | |
44f3bcd2 | 2960 | read_data[size] = 0; |
3ab651fc | 2961 | data = (char *)read_data; |
49b6d722 LL |
2962 | } |
2963 | ||
3ab651fc | 2964 | return data; |
49b6d722 LL |
2965 | } |
2966 | ||
33521634 | 2967 | QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename) |
191bc01b | 2968 | { |
6ea314d9 | 2969 | char host[65], port[33], width[8], height[8]; |
aeb2c47a | 2970 | int pos; |
7d31544f | 2971 | const char *p; |
191bc01b | 2972 | QemuOpts *opts; |
8be7e7e4 | 2973 | Error *local_err = NULL; |
191bc01b | 2974 | |
8be7e7e4 LC |
2975 | opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err); |
2976 | if (error_is_set(&local_err)) { | |
2977 | qerror_report_err(local_err); | |
2978 | error_free(local_err); | |
191bc01b | 2979 | return NULL; |
8be7e7e4 | 2980 | } |
191bc01b | 2981 | |
7591c5c1 GH |
2982 | if (strstart(filename, "mon:", &p)) { |
2983 | filename = p; | |
2984 | qemu_opt_set(opts, "mux", "on"); | |
2985 | } | |
2986 | ||
f0457e8d GH |
2987 | if (strcmp(filename, "null") == 0 || |
2988 | strcmp(filename, "pty") == 0 || | |
2989 | strcmp(filename, "msmouse") == 0 || | |
dc1c21e6 | 2990 | strcmp(filename, "braille") == 0 || |
f0457e8d | 2991 | strcmp(filename, "stdio") == 0) { |
4490dadf | 2992 | qemu_opt_set(opts, "backend", filename); |
191bc01b GH |
2993 | return opts; |
2994 | } | |
6ea314d9 GH |
2995 | if (strstart(filename, "vc", &p)) { |
2996 | qemu_opt_set(opts, "backend", "vc"); | |
2997 | if (*p == ':') { | |
2998 | if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) { | |
2999 | /* pixels */ | |
3000 | qemu_opt_set(opts, "width", width); | |
3001 | qemu_opt_set(opts, "height", height); | |
3002 | } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) { | |
3003 | /* chars */ | |
3004 | qemu_opt_set(opts, "cols", width); | |
3005 | qemu_opt_set(opts, "rows", height); | |
3006 | } else { | |
3007 | goto fail; | |
3008 | } | |
3009 | } | |
3010 | return opts; | |
3011 | } | |
d6c983cd GH |
3012 | if (strcmp(filename, "con:") == 0) { |
3013 | qemu_opt_set(opts, "backend", "console"); | |
3014 | return opts; | |
3015 | } | |
48b76496 GH |
3016 | if (strstart(filename, "COM", NULL)) { |
3017 | qemu_opt_set(opts, "backend", "serial"); | |
3018 | qemu_opt_set(opts, "path", filename); | |
3019 | return opts; | |
3020 | } | |
7d31544f GH |
3021 | if (strstart(filename, "file:", &p)) { |
3022 | qemu_opt_set(opts, "backend", "file"); | |
3023 | qemu_opt_set(opts, "path", p); | |
3024 | return opts; | |
3025 | } | |
3026 | if (strstart(filename, "pipe:", &p)) { | |
3027 | qemu_opt_set(opts, "backend", "pipe"); | |
3028 | qemu_opt_set(opts, "path", p); | |
3029 | return opts; | |
3030 | } | |
aeb2c47a GH |
3031 | if (strstart(filename, "tcp:", &p) || |
3032 | strstart(filename, "telnet:", &p)) { | |
3033 | if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { | |
3034 | host[0] = 0; | |
3035 | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) | |
3036 | goto fail; | |
3037 | } | |
3038 | qemu_opt_set(opts, "backend", "socket"); | |
3039 | qemu_opt_set(opts, "host", host); | |
3040 | qemu_opt_set(opts, "port", port); | |
3041 | if (p[pos] == ',') { | |
3042 | if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0) | |
3043 | goto fail; | |
3044 | } | |
3045 | if (strstart(filename, "telnet:", &p)) | |
3046 | qemu_opt_set(opts, "telnet", "on"); | |
3047 | return opts; | |
3048 | } | |
7e1b35b4 GH |
3049 | if (strstart(filename, "udp:", &p)) { |
3050 | qemu_opt_set(opts, "backend", "udp"); | |
3051 | if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) { | |
3052 | host[0] = 0; | |
39324ca4 | 3053 | if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) { |
7e1b35b4 GH |
3054 | goto fail; |
3055 | } | |
3056 | } | |
3057 | qemu_opt_set(opts, "host", host); | |
3058 | qemu_opt_set(opts, "port", port); | |
3059 | if (p[pos] == '@') { | |
3060 | p += pos + 1; | |
3061 | if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { | |
3062 | host[0] = 0; | |
3063 | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) { | |
7e1b35b4 GH |
3064 | goto fail; |
3065 | } | |
3066 | } | |
3067 | qemu_opt_set(opts, "localaddr", host); | |
3068 | qemu_opt_set(opts, "localport", port); | |
3069 | } | |
3070 | return opts; | |
3071 | } | |
aeb2c47a GH |
3072 | if (strstart(filename, "unix:", &p)) { |
3073 | qemu_opt_set(opts, "backend", "socket"); | |
3074 | if (qemu_opts_do_parse(opts, p, "path") != 0) | |
3075 | goto fail; | |
3076 | return opts; | |
3077 | } | |
48b76496 GH |
3078 | if (strstart(filename, "/dev/parport", NULL) || |
3079 | strstart(filename, "/dev/ppi", NULL)) { | |
3080 | qemu_opt_set(opts, "backend", "parport"); | |
3081 | qemu_opt_set(opts, "path", filename); | |
3082 | return opts; | |
3083 | } | |
3084 | if (strstart(filename, "/dev/", NULL)) { | |
3085 | qemu_opt_set(opts, "backend", "tty"); | |
3086 | qemu_opt_set(opts, "path", filename); | |
3087 | return opts; | |
3088 | } | |
191bc01b | 3089 | |
aeb2c47a | 3090 | fail: |
191bc01b GH |
3091 | qemu_opts_del(opts); |
3092 | return NULL; | |
3093 | } | |
3094 | ||
846e2e49 GH |
3095 | static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend, |
3096 | Error **errp) | |
3097 | { | |
3098 | const char *path = qemu_opt_get(opts, "path"); | |
3099 | ||
3100 | if (path == NULL) { | |
3101 | error_setg(errp, "chardev: file: no filename given"); | |
3102 | return; | |
3103 | } | |
3104 | backend->file = g_new0(ChardevFile, 1); | |
3105 | backend->file->out = g_strdup(path); | |
3106 | } | |
3107 | ||
7c358031 GH |
3108 | static void qemu_chr_parse_stdio(QemuOpts *opts, ChardevBackend *backend, |
3109 | Error **errp) | |
3110 | { | |
3111 | backend->stdio = g_new0(ChardevStdio, 1); | |
3112 | backend->stdio->has_signal = true; | |
3113 | backend->stdio->signal = | |
3114 | qemu_opt_get_bool(opts, "signal", display_type != DT_NOGRAPHIC); | |
3115 | } | |
3116 | ||
0f1cb51d GH |
3117 | static void qemu_chr_parse_serial(QemuOpts *opts, ChardevBackend *backend, |
3118 | Error **errp) | |
3119 | { | |
3120 | const char *device = qemu_opt_get(opts, "path"); | |
3121 | ||
3122 | if (device == NULL) { | |
3123 | error_setg(errp, "chardev: serial/tty: no device path given"); | |
3124 | return; | |
3125 | } | |
3126 | backend->serial = g_new0(ChardevHostdev, 1); | |
3127 | backend->serial->device = g_strdup(device); | |
3128 | } | |
3129 | ||
dc375097 GH |
3130 | static void qemu_chr_parse_parallel(QemuOpts *opts, ChardevBackend *backend, |
3131 | Error **errp) | |
3132 | { | |
3133 | const char *device = qemu_opt_get(opts, "path"); | |
3134 | ||
3135 | if (device == NULL) { | |
3136 | error_setg(errp, "chardev: parallel: no device path given"); | |
3137 | return; | |
3138 | } | |
3139 | backend->parallel = g_new0(ChardevHostdev, 1); | |
3140 | backend->parallel->device = g_strdup(device); | |
3141 | } | |
3142 | ||
548cbb36 GH |
3143 | static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend, |
3144 | Error **errp) | |
3145 | { | |
3146 | const char *device = qemu_opt_get(opts, "path"); | |
3147 | ||
3148 | if (device == NULL) { | |
3149 | error_setg(errp, "chardev: pipe: no device path given"); | |
3150 | return; | |
3151 | } | |
3152 | backend->pipe = g_new0(ChardevHostdev, 1); | |
3153 | backend->pipe->device = g_strdup(device); | |
3154 | } | |
3155 | ||
1da48c65 GH |
3156 | static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend, |
3157 | Error **errp) | |
3158 | { | |
3159 | int val; | |
3160 | ||
3161 | backend->memory = g_new0(ChardevRingbuf, 1); | |
3162 | ||
3163 | val = qemu_opt_get_number(opts, "size", 0); | |
3164 | if (val != 0) { | |
3165 | backend->memory->has_size = true; | |
3166 | backend->memory->size = val; | |
3167 | } | |
3168 | } | |
3169 | ||
d654f34e | 3170 | typedef struct CharDriver { |
191bc01b | 3171 | const char *name; |
2c5f4882 | 3172 | /* old, pre qapi */ |
1f51470d | 3173 | CharDriverState *(*open)(QemuOpts *opts); |
2c5f4882 GH |
3174 | /* new, qapi-based */ |
3175 | int kind; | |
3176 | void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp); | |
d654f34e AL |
3177 | } CharDriver; |
3178 | ||
3179 | static GSList *backends; | |
3180 | ||
3181 | void register_char_driver(const char *name, CharDriverState *(*open)(QemuOpts *)) | |
3182 | { | |
3183 | CharDriver *s; | |
3184 | ||
3185 | s = g_malloc0(sizeof(*s)); | |
3186 | s->name = g_strdup(name); | |
3187 | s->open = open; | |
3188 | ||
3189 | backends = g_slist_append(backends, s); | |
3190 | } | |
191bc01b | 3191 | |
2c5f4882 GH |
3192 | void register_char_driver_qapi(const char *name, int kind, |
3193 | void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp)) | |
3194 | { | |
3195 | CharDriver *s; | |
3196 | ||
3197 | s = g_malloc0(sizeof(*s)); | |
3198 | s->name = g_strdup(name); | |
3199 | s->kind = kind; | |
3200 | s->parse = parse; | |
3201 | ||
3202 | backends = g_slist_append(backends, s); | |
3203 | } | |
3204 | ||
f69554b9 | 3205 | CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts, |
bd2d80b2 GH |
3206 | void (*init)(struct CharDriverState *s), |
3207 | Error **errp) | |
191bc01b | 3208 | { |
d654f34e | 3209 | CharDriver *cd; |
191bc01b | 3210 | CharDriverState *chr; |
d654f34e | 3211 | GSList *i; |
191bc01b GH |
3212 | |
3213 | if (qemu_opts_id(opts) == NULL) { | |
312fd5f2 | 3214 | error_setg(errp, "chardev: no id specified"); |
2274ae9d | 3215 | goto err; |
191bc01b GH |
3216 | } |
3217 | ||
1bbd185f | 3218 | if (qemu_opt_get(opts, "backend") == NULL) { |
312fd5f2 | 3219 | error_setg(errp, "chardev: \"%s\" missing backend", |
bd2d80b2 | 3220 | qemu_opts_id(opts)); |
2274ae9d | 3221 | goto err; |
1bbd185f | 3222 | } |
d654f34e AL |
3223 | for (i = backends; i; i = i->next) { |
3224 | cd = i->data; | |
3225 | ||
3226 | if (strcmp(cd->name, qemu_opt_get(opts, "backend")) == 0) { | |
191bc01b | 3227 | break; |
d654f34e | 3228 | } |
191bc01b | 3229 | } |
d654f34e | 3230 | if (i == NULL) { |
312fd5f2 | 3231 | error_setg(errp, "chardev: backend \"%s\" not found", |
bd2d80b2 | 3232 | qemu_opt_get(opts, "backend")); |
d654f34e | 3233 | return NULL; |
191bc01b GH |
3234 | } |
3235 | ||
2c5f4882 GH |
3236 | if (!cd->open) { |
3237 | /* using new, qapi init */ | |
3238 | ChardevBackend *backend = g_new0(ChardevBackend, 1); | |
3239 | ChardevReturn *ret = NULL; | |
3240 | const char *id = qemu_opts_id(opts); | |
edb2fb3c GH |
3241 | const char *bid = NULL; |
3242 | ||
3243 | if (qemu_opt_get_bool(opts, "mux", 0)) { | |
3244 | bid = g_strdup_printf("%s-base", id); | |
3245 | } | |
2c5f4882 GH |
3246 | |
3247 | chr = NULL; | |
3248 | backend->kind = cd->kind; | |
3249 | if (cd->parse) { | |
3250 | cd->parse(opts, backend, errp); | |
3251 | if (error_is_set(errp)) { | |
3252 | goto qapi_out; | |
3253 | } | |
3254 | } | |
edb2fb3c | 3255 | ret = qmp_chardev_add(bid ? bid : id, backend, errp); |
2c5f4882 GH |
3256 | if (error_is_set(errp)) { |
3257 | goto qapi_out; | |
3258 | } | |
edb2fb3c GH |
3259 | |
3260 | if (bid) { | |
3261 | qapi_free_ChardevBackend(backend); | |
3262 | qapi_free_ChardevReturn(ret); | |
3263 | backend = g_new0(ChardevBackend, 1); | |
3264 | backend->mux = g_new0(ChardevMux, 1); | |
3265 | backend->kind = CHARDEV_BACKEND_KIND_MUX; | |
3266 | backend->mux->chardev = g_strdup(bid); | |
3267 | ret = qmp_chardev_add(id, backend, errp); | |
3268 | if (error_is_set(errp)) { | |
3269 | goto qapi_out; | |
3270 | } | |
3271 | } | |
3272 | ||
2c5f4882 GH |
3273 | chr = qemu_chr_find(id); |
3274 | ||
3275 | qapi_out: | |
3276 | qapi_free_ChardevBackend(backend); | |
3277 | qapi_free_ChardevReturn(ret); | |
3278 | return chr; | |
3279 | } | |
3280 | ||
d654f34e | 3281 | chr = cd->open(opts); |
1f51470d | 3282 | if (!chr) { |
312fd5f2 | 3283 | error_setg(errp, "chardev: opening backend \"%s\" failed", |
bd2d80b2 | 3284 | qemu_opt_get(opts, "backend")); |
2274ae9d | 3285 | goto err; |
191bc01b GH |
3286 | } |
3287 | ||
3288 | if (!chr->filename) | |
7267c094 | 3289 | chr->filename = g_strdup(qemu_opt_get(opts, "backend")); |
191bc01b | 3290 | chr->init = init; |
72cf2d4f | 3291 | QTAILQ_INSERT_TAIL(&chardevs, chr, next); |
7591c5c1 GH |
3292 | |
3293 | if (qemu_opt_get_bool(opts, "mux", 0)) { | |
3294 | CharDriverState *base = chr; | |
3295 | int len = strlen(qemu_opts_id(opts)) + 6; | |
7267c094 | 3296 | base->label = g_malloc(len); |
7591c5c1 GH |
3297 | snprintf(base->label, len, "%s-base", qemu_opts_id(opts)); |
3298 | chr = qemu_chr_open_mux(base); | |
3299 | chr->filename = base->filename; | |
d5b27167 | 3300 | chr->avail_connections = MAX_MUX; |
72cf2d4f | 3301 | QTAILQ_INSERT_TAIL(&chardevs, chr, next); |
d5b27167 KK |
3302 | } else { |
3303 | chr->avail_connections = 1; | |
7591c5c1 | 3304 | } |
7267c094 | 3305 | chr->label = g_strdup(qemu_opts_id(opts)); |
2274ae9d | 3306 | chr->opts = opts; |
191bc01b | 3307 | return chr; |
2274ae9d GH |
3308 | |
3309 | err: | |
3310 | qemu_opts_del(opts); | |
3311 | return NULL; | |
191bc01b GH |
3312 | } |
3313 | ||
27143a44 | 3314 | CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s)) |
6f97dba0 AL |
3315 | { |
3316 | const char *p; | |
3317 | CharDriverState *chr; | |
191bc01b | 3318 | QemuOpts *opts; |
bd2d80b2 | 3319 | Error *err = NULL; |
191bc01b | 3320 | |
c845f401 GH |
3321 | if (strstart(filename, "chardev:", &p)) { |
3322 | return qemu_chr_find(p); | |
3323 | } | |
3324 | ||
191bc01b | 3325 | opts = qemu_chr_parse_compat(label, filename); |
7e1b35b4 GH |
3326 | if (!opts) |
3327 | return NULL; | |
6f97dba0 | 3328 | |
bd2d80b2 GH |
3329 | chr = qemu_chr_new_from_opts(opts, init, &err); |
3330 | if (error_is_set(&err)) { | |
3331 | fprintf(stderr, "%s\n", error_get_pretty(err)); | |
3332 | error_free(err); | |
3333 | } | |
7e1b35b4 | 3334 | if (chr && qemu_opt_get_bool(opts, "mux", 0)) { |
456d6069 | 3335 | qemu_chr_fe_claim_no_fail(chr); |
7e1b35b4 | 3336 | monitor_init(chr, MONITOR_USE_READLINE); |
6f97dba0 AL |
3337 | } |
3338 | return chr; | |
3339 | } | |
3340 | ||
15f31519 | 3341 | void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo) |
c48855e1 PB |
3342 | { |
3343 | if (chr->chr_set_echo) { | |
3344 | chr->chr_set_echo(chr, echo); | |
3345 | } | |
3346 | } | |
3347 | ||
8e25daa8 | 3348 | void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open) |
7c32c4fe | 3349 | { |
8e25daa8 | 3350 | if (chr->fe_open == fe_open) { |
c0c4bd2c HG |
3351 | return; |
3352 | } | |
8e25daa8 | 3353 | chr->fe_open = fe_open; |
574b711a HG |
3354 | if (chr->chr_set_fe_open) { |
3355 | chr->chr_set_fe_open(chr, fe_open); | |
7c32c4fe HG |
3356 | } |
3357 | } | |
3358 | ||
2c8a5942 KW |
3359 | int qemu_chr_fe_add_watch(CharDriverState *s, GIOCondition cond, |
3360 | GIOFunc func, void *user_data) | |
23673ca7 AL |
3361 | { |
3362 | GSource *src; | |
3363 | guint tag; | |
3364 | ||
3365 | if (s->chr_add_watch == NULL) { | |
3366 | return -ENOSYS; | |
3367 | } | |
3368 | ||
3369 | src = s->chr_add_watch(s, cond); | |
3370 | g_source_set_callback(src, (GSourceFunc)func, user_data, NULL); | |
3371 | tag = g_source_attach(src, NULL); | |
3372 | g_source_unref(src); | |
3373 | ||
3374 | return tag; | |
3375 | } | |
3376 | ||
44c473de HG |
3377 | int qemu_chr_fe_claim(CharDriverState *s) |
3378 | { | |
3379 | if (s->avail_connections < 1) { | |
3380 | return -1; | |
3381 | } | |
3382 | s->avail_connections--; | |
3383 | return 0; | |
3384 | } | |
3385 | ||
3386 | void qemu_chr_fe_claim_no_fail(CharDriverState *s) | |
3387 | { | |
3388 | if (qemu_chr_fe_claim(s) != 0) { | |
3389 | fprintf(stderr, "%s: error chardev \"%s\" already used\n", | |
3390 | __func__, s->label); | |
3391 | exit(1); | |
3392 | } | |
3393 | } | |
3394 | ||
3395 | void qemu_chr_fe_release(CharDriverState *s) | |
3396 | { | |
3397 | s->avail_connections++; | |
3398 | } | |
3399 | ||
70f24fb6 | 3400 | void qemu_chr_delete(CharDriverState *chr) |
6f97dba0 | 3401 | { |
72cf2d4f | 3402 | QTAILQ_REMOVE(&chardevs, chr, next); |
2274ae9d | 3403 | if (chr->chr_close) { |
6f97dba0 | 3404 | chr->chr_close(chr); |
2274ae9d | 3405 | } |
7267c094 AL |
3406 | g_free(chr->filename); |
3407 | g_free(chr->label); | |
2274ae9d GH |
3408 | if (chr->opts) { |
3409 | qemu_opts_del(chr->opts); | |
3410 | } | |
7267c094 | 3411 | g_free(chr); |
6f97dba0 AL |
3412 | } |
3413 | ||
c5a415a0 | 3414 | ChardevInfoList *qmp_query_chardev(Error **errp) |
6f97dba0 | 3415 | { |
c5a415a0 | 3416 | ChardevInfoList *chr_list = NULL; |
6f97dba0 AL |
3417 | CharDriverState *chr; |
3418 | ||
72cf2d4f | 3419 | QTAILQ_FOREACH(chr, &chardevs, next) { |
c5a415a0 LC |
3420 | ChardevInfoList *info = g_malloc0(sizeof(*info)); |
3421 | info->value = g_malloc0(sizeof(*info->value)); | |
3422 | info->value->label = g_strdup(chr->label); | |
3423 | info->value->filename = g_strdup(chr->filename); | |
3424 | ||
3425 | info->next = chr_list; | |
3426 | chr_list = info; | |
6f97dba0 | 3427 | } |
588b3832 | 3428 | |
c5a415a0 | 3429 | return chr_list; |
6f97dba0 | 3430 | } |
c845f401 GH |
3431 | |
3432 | CharDriverState *qemu_chr_find(const char *name) | |
3433 | { | |
3434 | CharDriverState *chr; | |
3435 | ||
72cf2d4f | 3436 | QTAILQ_FOREACH(chr, &chardevs, next) { |
c845f401 GH |
3437 | if (strcmp(chr->label, name) != 0) |
3438 | continue; | |
3439 | return chr; | |
3440 | } | |
3441 | return NULL; | |
3442 | } | |
0beb4942 AL |
3443 | |
3444 | /* Get a character (serial) device interface. */ | |
3445 | CharDriverState *qemu_char_get_next_serial(void) | |
3446 | { | |
3447 | static int next_serial; | |
456d6069 | 3448 | CharDriverState *chr; |
0beb4942 AL |
3449 | |
3450 | /* FIXME: This function needs to go away: use chardev properties! */ | |
456d6069 HG |
3451 | |
3452 | while (next_serial < MAX_SERIAL_PORTS && serial_hds[next_serial]) { | |
3453 | chr = serial_hds[next_serial++]; | |
3454 | qemu_chr_fe_claim_no_fail(chr); | |
3455 | return chr; | |
3456 | } | |
3457 | return NULL; | |
0beb4942 AL |
3458 | } |
3459 | ||
4d454574 PB |
3460 | QemuOptsList qemu_chardev_opts = { |
3461 | .name = "chardev", | |
3462 | .implied_opt_name = "backend", | |
3463 | .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head), | |
3464 | .desc = { | |
3465 | { | |
3466 | .name = "backend", | |
3467 | .type = QEMU_OPT_STRING, | |
3468 | },{ | |
3469 | .name = "path", | |
3470 | .type = QEMU_OPT_STRING, | |
3471 | },{ | |
3472 | .name = "host", | |
3473 | .type = QEMU_OPT_STRING, | |
3474 | },{ | |
3475 | .name = "port", | |
3476 | .type = QEMU_OPT_STRING, | |
3477 | },{ | |
3478 | .name = "localaddr", | |
3479 | .type = QEMU_OPT_STRING, | |
3480 | },{ | |
3481 | .name = "localport", | |
3482 | .type = QEMU_OPT_STRING, | |
3483 | },{ | |
3484 | .name = "to", | |
3485 | .type = QEMU_OPT_NUMBER, | |
3486 | },{ | |
3487 | .name = "ipv4", | |
3488 | .type = QEMU_OPT_BOOL, | |
3489 | },{ | |
3490 | .name = "ipv6", | |
3491 | .type = QEMU_OPT_BOOL, | |
3492 | },{ | |
3493 | .name = "wait", | |
3494 | .type = QEMU_OPT_BOOL, | |
3495 | },{ | |
3496 | .name = "server", | |
3497 | .type = QEMU_OPT_BOOL, | |
3498 | },{ | |
3499 | .name = "delay", | |
3500 | .type = QEMU_OPT_BOOL, | |
3501 | },{ | |
3502 | .name = "telnet", | |
3503 | .type = QEMU_OPT_BOOL, | |
3504 | },{ | |
3505 | .name = "width", | |
3506 | .type = QEMU_OPT_NUMBER, | |
3507 | },{ | |
3508 | .name = "height", | |
3509 | .type = QEMU_OPT_NUMBER, | |
3510 | },{ | |
3511 | .name = "cols", | |
3512 | .type = QEMU_OPT_NUMBER, | |
3513 | },{ | |
3514 | .name = "rows", | |
3515 | .type = QEMU_OPT_NUMBER, | |
3516 | },{ | |
3517 | .name = "mux", | |
3518 | .type = QEMU_OPT_BOOL, | |
3519 | },{ | |
3520 | .name = "signal", | |
3521 | .type = QEMU_OPT_BOOL, | |
3522 | },{ | |
3523 | .name = "name", | |
3524 | .type = QEMU_OPT_STRING, | |
3525 | },{ | |
3526 | .name = "debug", | |
3527 | .type = QEMU_OPT_NUMBER, | |
51767e7c | 3528 | },{ |
3949e594 | 3529 | .name = "size", |
de1cc36e | 3530 | .type = QEMU_OPT_SIZE, |
4d454574 PB |
3531 | }, |
3532 | { /* end of list */ } | |
3533 | }, | |
3534 | }; | |
f1a1a356 | 3535 | |
ffbdbe59 GH |
3536 | #ifdef _WIN32 |
3537 | ||
3538 | static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp) | |
3539 | { | |
3540 | HANDLE out; | |
3541 | ||
3542 | if (file->in) { | |
3543 | error_setg(errp, "input file not supported"); | |
3544 | return NULL; | |
3545 | } | |
3546 | ||
3547 | out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL, | |
3548 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | |
3549 | if (out == INVALID_HANDLE_VALUE) { | |
3550 | error_setg(errp, "open %s failed", file->out); | |
3551 | return NULL; | |
3552 | } | |
3553 | return qemu_chr_open_win_file(out); | |
3554 | } | |
3555 | ||
d36b2b90 MA |
3556 | static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial, |
3557 | Error **errp) | |
d59044ef | 3558 | { |
d36b2b90 MA |
3559 | return qemu_chr_open_win_path(serial->device); |
3560 | } | |
3561 | ||
3562 | static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel, | |
3563 | Error **errp) | |
3564 | { | |
3565 | error_setg(errp, "character device backend type 'parallel' not supported"); | |
3566 | return NULL; | |
d59044ef GH |
3567 | } |
3568 | ||
ffbdbe59 GH |
3569 | #else /* WIN32 */ |
3570 | ||
3571 | static int qmp_chardev_open_file_source(char *src, int flags, | |
3572 | Error **errp) | |
3573 | { | |
3574 | int fd = -1; | |
3575 | ||
3576 | TFR(fd = qemu_open(src, flags, 0666)); | |
3577 | if (fd == -1) { | |
3578 | error_setg(errp, "open %s: %s", src, strerror(errno)); | |
3579 | } | |
3580 | return fd; | |
3581 | } | |
3582 | ||
3583 | static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp) | |
3584 | { | |
3585 | int flags, in = -1, out = -1; | |
3586 | ||
3587 | flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY; | |
3588 | out = qmp_chardev_open_file_source(file->out, flags, errp); | |
3589 | if (error_is_set(errp)) { | |
3590 | return NULL; | |
3591 | } | |
3592 | ||
3593 | if (file->in) { | |
3594 | flags = O_RDONLY; | |
3595 | in = qmp_chardev_open_file_source(file->in, flags, errp); | |
3596 | if (error_is_set(errp)) { | |
3597 | qemu_close(out); | |
3598 | return NULL; | |
3599 | } | |
3600 | } | |
3601 | ||
3602 | return qemu_chr_open_fd(in, out); | |
3603 | } | |
3604 | ||
d36b2b90 MA |
3605 | static CharDriverState *qmp_chardev_open_serial(ChardevHostdev *serial, |
3606 | Error **errp) | |
d59044ef | 3607 | { |
d59044ef | 3608 | #ifdef HAVE_CHARDEV_TTY |
d36b2b90 MA |
3609 | int fd; |
3610 | ||
3611 | fd = qmp_chardev_open_file_source(serial->device, O_RDWR, errp); | |
3612 | if (error_is_set(errp)) { | |
3613 | return NULL; | |
39099991 | 3614 | } |
f9e8cacc | 3615 | qemu_set_nonblock(fd); |
d36b2b90 MA |
3616 | return qemu_chr_open_tty_fd(fd); |
3617 | #else | |
3618 | error_setg(errp, "character device backend type 'serial' not supported"); | |
3619 | return NULL; | |
88a946d3 | 3620 | #endif |
d36b2b90 MA |
3621 | } |
3622 | ||
3623 | static CharDriverState *qmp_chardev_open_parallel(ChardevHostdev *parallel, | |
3624 | Error **errp) | |
3625 | { | |
88a946d3 | 3626 | #ifdef HAVE_CHARDEV_PARPORT |
d36b2b90 MA |
3627 | int fd; |
3628 | ||
3629 | fd = qmp_chardev_open_file_source(parallel->device, O_RDWR, errp); | |
3630 | if (error_is_set(errp)) { | |
d59044ef GH |
3631 | return NULL; |
3632 | } | |
d36b2b90 MA |
3633 | return qemu_chr_open_pp_fd(fd); |
3634 | #else | |
3635 | error_setg(errp, "character device backend type 'parallel' not supported"); | |
3636 | return NULL; | |
3637 | #endif | |
d59044ef GH |
3638 | } |
3639 | ||
ffbdbe59 GH |
3640 | #endif /* WIN32 */ |
3641 | ||
f6bd5d6e GH |
3642 | static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock, |
3643 | Error **errp) | |
3644 | { | |
3645 | SocketAddress *addr = sock->addr; | |
3646 | bool do_nodelay = sock->has_nodelay ? sock->nodelay : false; | |
3647 | bool is_listen = sock->has_server ? sock->server : true; | |
3648 | bool is_telnet = sock->has_telnet ? sock->telnet : false; | |
3649 | bool is_waitconnect = sock->has_wait ? sock->wait : false; | |
3650 | int fd; | |
3651 | ||
3652 | if (is_listen) { | |
3653 | fd = socket_listen(addr, errp); | |
3654 | } else { | |
3655 | fd = socket_connect(addr, errp, NULL, NULL); | |
3656 | } | |
3657 | if (error_is_set(errp)) { | |
3658 | return NULL; | |
3659 | } | |
3660 | return qemu_chr_open_socket_fd(fd, do_nodelay, is_listen, | |
3661 | is_telnet, is_waitconnect, errp); | |
3662 | } | |
3663 | ||
3ecc059d GH |
3664 | static CharDriverState *qmp_chardev_open_dgram(ChardevDgram *dgram, |
3665 | Error **errp) | |
3666 | { | |
3667 | int fd; | |
3668 | ||
3669 | fd = socket_dgram(dgram->remote, dgram->local, errp); | |
3670 | if (error_is_set(errp)) { | |
3671 | return NULL; | |
3672 | } | |
3673 | return qemu_chr_open_udp_fd(fd); | |
3674 | } | |
3675 | ||
f1a1a356 GH |
3676 | ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, |
3677 | Error **errp) | |
3678 | { | |
3679 | ChardevReturn *ret = g_new0(ChardevReturn, 1); | |
edb2fb3c | 3680 | CharDriverState *base, *chr = NULL; |
f1a1a356 GH |
3681 | |
3682 | chr = qemu_chr_find(id); | |
3683 | if (chr) { | |
3684 | error_setg(errp, "Chardev '%s' already exists", id); | |
3685 | g_free(ret); | |
3686 | return NULL; | |
3687 | } | |
3688 | ||
3689 | switch (backend->kind) { | |
ffbdbe59 GH |
3690 | case CHARDEV_BACKEND_KIND_FILE: |
3691 | chr = qmp_chardev_open_file(backend->file, errp); | |
3692 | break; | |
d36b2b90 MA |
3693 | case CHARDEV_BACKEND_KIND_SERIAL: |
3694 | chr = qmp_chardev_open_serial(backend->serial, errp); | |
3695 | break; | |
3696 | case CHARDEV_BACKEND_KIND_PARALLEL: | |
3697 | chr = qmp_chardev_open_parallel(backend->parallel, errp); | |
d59044ef | 3698 | break; |
548cbb36 GH |
3699 | case CHARDEV_BACKEND_KIND_PIPE: |
3700 | chr = qemu_chr_open_pipe(backend->pipe); | |
3701 | break; | |
f6bd5d6e GH |
3702 | case CHARDEV_BACKEND_KIND_SOCKET: |
3703 | chr = qmp_chardev_open_socket(backend->socket, errp); | |
3704 | break; | |
3ecc059d GH |
3705 | case CHARDEV_BACKEND_KIND_DGRAM: |
3706 | chr = qmp_chardev_open_dgram(backend->dgram, errp); | |
3707 | break; | |
0a1a7fab GH |
3708 | #ifdef HAVE_CHARDEV_TTY |
3709 | case CHARDEV_BACKEND_KIND_PTY: | |
e68c5958 | 3710 | chr = qemu_chr_open_pty(id, ret); |
0a1a7fab | 3711 | break; |
0a1a7fab | 3712 | #endif |
f1a1a356 | 3713 | case CHARDEV_BACKEND_KIND_NULL: |
80dca9e6 | 3714 | chr = qemu_chr_open_null(); |
f1a1a356 | 3715 | break; |
edb2fb3c GH |
3716 | case CHARDEV_BACKEND_KIND_MUX: |
3717 | base = qemu_chr_find(backend->mux->chardev); | |
3718 | if (base == NULL) { | |
3719 | error_setg(errp, "mux: base chardev %s not found", | |
3720 | backend->mux->chardev); | |
3721 | break; | |
3722 | } | |
3723 | chr = qemu_chr_open_mux(base); | |
3724 | break; | |
f5a51cab GH |
3725 | case CHARDEV_BACKEND_KIND_MSMOUSE: |
3726 | chr = qemu_chr_open_msmouse(); | |
3727 | break; | |
2d57286d GH |
3728 | #ifdef CONFIG_BRLAPI |
3729 | case CHARDEV_BACKEND_KIND_BRAILLE: | |
3730 | chr = chr_baum_init(); | |
3731 | break; | |
3732 | #endif | |
7c358031 GH |
3733 | case CHARDEV_BACKEND_KIND_STDIO: |
3734 | chr = qemu_chr_open_stdio(backend->stdio); | |
3735 | break; | |
d9ac374f GH |
3736 | #ifdef _WIN32 |
3737 | case CHARDEV_BACKEND_KIND_CONSOLE: | |
3738 | chr = qemu_chr_open_win_con(); | |
3739 | break; | |
cd153e2a GH |
3740 | #endif |
3741 | #ifdef CONFIG_SPICE | |
3742 | case CHARDEV_BACKEND_KIND_SPICEVMC: | |
3743 | chr = qemu_chr_open_spice_vmc(backend->spicevmc->type); | |
3744 | break; | |
3745 | case CHARDEV_BACKEND_KIND_SPICEPORT: | |
3746 | chr = qemu_chr_open_spice_port(backend->spiceport->fqdn); | |
3747 | break; | |
d9ac374f | 3748 | #endif |
702ec69c GH |
3749 | case CHARDEV_BACKEND_KIND_VC: |
3750 | chr = vc_init(backend->vc); | |
3751 | break; | |
1da48c65 GH |
3752 | case CHARDEV_BACKEND_KIND_MEMORY: |
3753 | chr = qemu_chr_open_ringbuf(backend->memory, errp); | |
3754 | break; | |
f1a1a356 GH |
3755 | default: |
3756 | error_setg(errp, "unknown chardev backend (%d)", backend->kind); | |
3757 | break; | |
3758 | } | |
3759 | ||
3760 | if (chr == NULL && !error_is_set(errp)) { | |
3761 | error_setg(errp, "Failed to create chardev"); | |
3762 | } | |
3763 | if (chr) { | |
3764 | chr->label = g_strdup(id); | |
edb2fb3c GH |
3765 | chr->avail_connections = |
3766 | (backend->kind == CHARDEV_BACKEND_KIND_MUX) ? MAX_MUX : 1; | |
f1a1a356 GH |
3767 | QTAILQ_INSERT_TAIL(&chardevs, chr, next); |
3768 | return ret; | |
3769 | } else { | |
3770 | g_free(ret); | |
3771 | return NULL; | |
3772 | } | |
3773 | } | |
3774 | ||
3775 | void qmp_chardev_remove(const char *id, Error **errp) | |
3776 | { | |
3777 | CharDriverState *chr; | |
3778 | ||
3779 | chr = qemu_chr_find(id); | |
3780 | if (NULL == chr) { | |
3781 | error_setg(errp, "Chardev '%s' not found", id); | |
3782 | return; | |
3783 | } | |
3784 | if (chr->chr_can_read || chr->chr_read || | |
3785 | chr->chr_event || chr->handler_opaque) { | |
3786 | error_setg(errp, "Chardev '%s' is busy", id); | |
3787 | return; | |
3788 | } | |
3789 | qemu_chr_delete(chr); | |
3790 | } | |
d654f34e AL |
3791 | |
3792 | static void register_types(void) | |
3793 | { | |
80dca9e6 | 3794 | register_char_driver_qapi("null", CHARDEV_BACKEND_KIND_NULL, NULL); |
d654f34e AL |
3795 | register_char_driver("socket", qemu_chr_open_socket); |
3796 | register_char_driver("udp", qemu_chr_open_udp); | |
1da48c65 GH |
3797 | register_char_driver_qapi("memory", CHARDEV_BACKEND_KIND_MEMORY, |
3798 | qemu_chr_parse_ringbuf); | |
846e2e49 GH |
3799 | register_char_driver_qapi("file", CHARDEV_BACKEND_KIND_FILE, |
3800 | qemu_chr_parse_file_out); | |
7c358031 GH |
3801 | register_char_driver_qapi("stdio", CHARDEV_BACKEND_KIND_STDIO, |
3802 | qemu_chr_parse_stdio); | |
0f1cb51d GH |
3803 | register_char_driver_qapi("serial", CHARDEV_BACKEND_KIND_SERIAL, |
3804 | qemu_chr_parse_serial); | |
3805 | register_char_driver_qapi("tty", CHARDEV_BACKEND_KIND_SERIAL, | |
3806 | qemu_chr_parse_serial); | |
dc375097 GH |
3807 | register_char_driver_qapi("parallel", CHARDEV_BACKEND_KIND_PARALLEL, |
3808 | qemu_chr_parse_parallel); | |
3809 | register_char_driver_qapi("parport", CHARDEV_BACKEND_KIND_PARALLEL, | |
3810 | qemu_chr_parse_parallel); | |
e68c5958 | 3811 | register_char_driver_qapi("pty", CHARDEV_BACKEND_KIND_PTY, NULL); |
d9ac374f | 3812 | register_char_driver_qapi("console", CHARDEV_BACKEND_KIND_CONSOLE, NULL); |
548cbb36 GH |
3813 | register_char_driver_qapi("pipe", CHARDEV_BACKEND_KIND_PIPE, |
3814 | qemu_chr_parse_pipe); | |
d654f34e AL |
3815 | } |
3816 | ||
3817 | type_init(register_types); |