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