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