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