root / qemu-char.c @ 651721b2
History | View | Annotate | Download (55.7 kB)
1 | 6f97dba0 | aliguori | /*
|
---|---|---|---|
2 | 6f97dba0 | aliguori | * QEMU System Emulator
|
3 | 6f97dba0 | aliguori | *
|
4 | 6f97dba0 | aliguori | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | 6f97dba0 | aliguori | *
|
6 | 6f97dba0 | aliguori | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 6f97dba0 | aliguori | * of this software and associated documentation files (the "Software"), to deal
|
8 | 6f97dba0 | aliguori | * in the Software without restriction, including without limitation the rights
|
9 | 6f97dba0 | aliguori | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 6f97dba0 | aliguori | * copies of the Software, and to permit persons to whom the Software is
|
11 | 6f97dba0 | aliguori | * furnished to do so, subject to the following conditions:
|
12 | 6f97dba0 | aliguori | *
|
13 | 6f97dba0 | aliguori | * The above copyright notice and this permission notice shall be included in
|
14 | 6f97dba0 | aliguori | * all copies or substantial portions of the Software.
|
15 | 6f97dba0 | aliguori | *
|
16 | 6f97dba0 | aliguori | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 6f97dba0 | aliguori | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 6f97dba0 | aliguori | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 6f97dba0 | aliguori | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 6f97dba0 | aliguori | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 6f97dba0 | aliguori | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 6f97dba0 | aliguori | * THE SOFTWARE.
|
23 | 6f97dba0 | aliguori | */
|
24 | 6f97dba0 | aliguori | #include "qemu-common.h" |
25 | 6f97dba0 | aliguori | #include "net.h" |
26 | 376253ec | aliguori | #include "monitor.h" |
27 | 6f97dba0 | aliguori | #include "console.h" |
28 | 6f97dba0 | aliguori | #include "sysemu.h" |
29 | 6f97dba0 | aliguori | #include "qemu-timer.h" |
30 | 6f97dba0 | aliguori | #include "qemu-char.h" |
31 | 6f97dba0 | aliguori | #include "block.h" |
32 | cf3ebac7 | aurel32 | #include "hw/usb.h" |
33 | cf3ebac7 | aurel32 | #include "hw/baum.h" |
34 | aa71cf80 | aurel32 | #include "hw/msmouse.h" |
35 | 6f97dba0 | aliguori | |
36 | 6f97dba0 | aliguori | #include <unistd.h> |
37 | 6f97dba0 | aliguori | #include <fcntl.h> |
38 | 6f97dba0 | aliguori | #include <signal.h> |
39 | 6f97dba0 | aliguori | #include <time.h> |
40 | 6f97dba0 | aliguori | #include <errno.h> |
41 | 6f97dba0 | aliguori | #include <sys/time.h> |
42 | 6f97dba0 | aliguori | #include <zlib.h> |
43 | 6f97dba0 | aliguori | |
44 | 6f97dba0 | aliguori | #ifndef _WIN32
|
45 | 6f97dba0 | aliguori | #include <sys/times.h> |
46 | 6f97dba0 | aliguori | #include <sys/wait.h> |
47 | 6f97dba0 | aliguori | #include <termios.h> |
48 | 6f97dba0 | aliguori | #include <sys/mman.h> |
49 | 6f97dba0 | aliguori | #include <sys/ioctl.h> |
50 | 24646c7e | blueswir1 | #include <sys/resource.h> |
51 | 6f97dba0 | aliguori | #include <sys/socket.h> |
52 | 6f97dba0 | aliguori | #include <netinet/in.h> |
53 | 24646c7e | blueswir1 | #include <net/if.h> |
54 | 24646c7e | blueswir1 | #ifdef __NetBSD__
|
55 | 24646c7e | blueswir1 | #include <net/if_tap.h> |
56 | 24646c7e | blueswir1 | #endif
|
57 | 24646c7e | blueswir1 | #ifdef __linux__
|
58 | 24646c7e | blueswir1 | #include <linux/if_tun.h> |
59 | 24646c7e | blueswir1 | #endif
|
60 | 24646c7e | blueswir1 | #include <arpa/inet.h> |
61 | 6f97dba0 | aliguori | #include <dirent.h> |
62 | 6f97dba0 | aliguori | #include <netdb.h> |
63 | 6f97dba0 | aliguori | #include <sys/select.h> |
64 | 179a2c19 | blueswir1 | #ifdef HOST_BSD
|
65 | 6f97dba0 | aliguori | #include <sys/stat.h> |
66 | 24646c7e | blueswir1 | #ifdef __FreeBSD__
|
67 | 6f97dba0 | aliguori | #include <libutil.h> |
68 | 6972f935 | blueswir1 | #include <dev/ppbus/ppi.h> |
69 | 6972f935 | blueswir1 | #include <dev/ppbus/ppbconf.h> |
70 | c5e97233 | blueswir1 | #elif defined(__DragonFly__)
|
71 | c5e97233 | blueswir1 | #include <libutil.h> |
72 | c5e97233 | blueswir1 | #include <dev/misc/ppi/ppi.h> |
73 | c5e97233 | blueswir1 | #include <bus/ppbus/ppbconf.h> |
74 | 24646c7e | blueswir1 | #else
|
75 | 24646c7e | blueswir1 | #include <util.h> |
76 | 6f97dba0 | aliguori | #endif
|
77 | 6f97dba0 | aliguori | #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
|
78 | 6f97dba0 | aliguori | #include <freebsd/stdlib.h> |
79 | 6f97dba0 | aliguori | #else
|
80 | 6f97dba0 | aliguori | #ifdef __linux__
|
81 | 6f97dba0 | aliguori | #include <pty.h> |
82 | 6f97dba0 | aliguori | |
83 | 6f97dba0 | aliguori | #include <linux/ppdev.h> |
84 | 6f97dba0 | aliguori | #include <linux/parport.h> |
85 | 6f97dba0 | aliguori | #endif
|
86 | 6f97dba0 | aliguori | #ifdef __sun__
|
87 | 6f97dba0 | aliguori | #include <sys/stat.h> |
88 | 6f97dba0 | aliguori | #include <sys/ethernet.h> |
89 | 6f97dba0 | aliguori | #include <sys/sockio.h> |
90 | 6f97dba0 | aliguori | #include <netinet/arp.h> |
91 | 6f97dba0 | aliguori | #include <netinet/in.h> |
92 | 6f97dba0 | aliguori | #include <netinet/in_systm.h> |
93 | 6f97dba0 | aliguori | #include <netinet/ip.h> |
94 | 6f97dba0 | aliguori | #include <netinet/ip_icmp.h> // must come after ip.h |
95 | 6f97dba0 | aliguori | #include <netinet/udp.h> |
96 | 6f97dba0 | aliguori | #include <netinet/tcp.h> |
97 | 6f97dba0 | aliguori | #include <net/if.h> |
98 | 6f97dba0 | aliguori | #include <syslog.h> |
99 | 6f97dba0 | aliguori | #include <stropts.h> |
100 | 6f97dba0 | aliguori | #endif
|
101 | 6f97dba0 | aliguori | #endif
|
102 | 6f97dba0 | aliguori | #endif
|
103 | 6f97dba0 | aliguori | |
104 | 6f97dba0 | aliguori | #include "qemu_socket.h" |
105 | 6f97dba0 | aliguori | |
106 | 6f97dba0 | aliguori | /***********************************************************/
|
107 | 6f97dba0 | aliguori | /* character device */
|
108 | 6f97dba0 | aliguori | |
109 | 2970a6c9 | aliguori | static TAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
|
110 | 2970a6c9 | aliguori | TAILQ_HEAD_INITIALIZER(chardevs); |
111 | 2970a6c9 | aliguori | static int initial_reset_issued; |
112 | 2970a6c9 | aliguori | |
113 | 6f97dba0 | aliguori | static void qemu_chr_event(CharDriverState *s, int event) |
114 | 6f97dba0 | aliguori | { |
115 | 6f97dba0 | aliguori | if (!s->chr_event)
|
116 | 6f97dba0 | aliguori | return;
|
117 | 6f97dba0 | aliguori | s->chr_event(s->handler_opaque, event); |
118 | 6f97dba0 | aliguori | } |
119 | 6f97dba0 | aliguori | |
120 | 6f97dba0 | aliguori | static void qemu_chr_reset_bh(void *opaque) |
121 | 6f97dba0 | aliguori | { |
122 | 6f97dba0 | aliguori | CharDriverState *s = opaque; |
123 | 6f97dba0 | aliguori | qemu_chr_event(s, CHR_EVENT_RESET); |
124 | 6f97dba0 | aliguori | qemu_bh_delete(s->bh); |
125 | 6f97dba0 | aliguori | s->bh = NULL;
|
126 | 6f97dba0 | aliguori | } |
127 | 6f97dba0 | aliguori | |
128 | 6f97dba0 | aliguori | void qemu_chr_reset(CharDriverState *s)
|
129 | 6f97dba0 | aliguori | { |
130 | 2970a6c9 | aliguori | if (s->bh == NULL && initial_reset_issued) { |
131 | 6f97dba0 | aliguori | s->bh = qemu_bh_new(qemu_chr_reset_bh, s); |
132 | 6f97dba0 | aliguori | qemu_bh_schedule(s->bh); |
133 | 6f97dba0 | aliguori | } |
134 | 6f97dba0 | aliguori | } |
135 | 6f97dba0 | aliguori | |
136 | 2970a6c9 | aliguori | void qemu_chr_initial_reset(void) |
137 | 2970a6c9 | aliguori | { |
138 | 2970a6c9 | aliguori | CharDriverState *chr; |
139 | 2970a6c9 | aliguori | |
140 | 2970a6c9 | aliguori | initial_reset_issued = 1;
|
141 | 2970a6c9 | aliguori | |
142 | 2970a6c9 | aliguori | TAILQ_FOREACH(chr, &chardevs, next) { |
143 | 2970a6c9 | aliguori | qemu_chr_reset(chr); |
144 | 2970a6c9 | aliguori | } |
145 | 2970a6c9 | aliguori | } |
146 | 2970a6c9 | aliguori | |
147 | 6f97dba0 | aliguori | int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len) |
148 | 6f97dba0 | aliguori | { |
149 | 6f97dba0 | aliguori | return s->chr_write(s, buf, len);
|
150 | 6f97dba0 | aliguori | } |
151 | 6f97dba0 | aliguori | |
152 | 6f97dba0 | aliguori | int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg) |
153 | 6f97dba0 | aliguori | { |
154 | 6f97dba0 | aliguori | if (!s->chr_ioctl)
|
155 | 6f97dba0 | aliguori | return -ENOTSUP;
|
156 | 6f97dba0 | aliguori | return s->chr_ioctl(s, cmd, arg);
|
157 | 6f97dba0 | aliguori | } |
158 | 6f97dba0 | aliguori | |
159 | 6f97dba0 | aliguori | int qemu_chr_can_read(CharDriverState *s)
|
160 | 6f97dba0 | aliguori | { |
161 | 6f97dba0 | aliguori | if (!s->chr_can_read)
|
162 | 6f97dba0 | aliguori | return 0; |
163 | 6f97dba0 | aliguori | return s->chr_can_read(s->handler_opaque);
|
164 | 6f97dba0 | aliguori | } |
165 | 6f97dba0 | aliguori | |
166 | 6f97dba0 | aliguori | void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len) |
167 | 6f97dba0 | aliguori | { |
168 | 6f97dba0 | aliguori | s->chr_read(s->handler_opaque, buf, len); |
169 | 6f97dba0 | aliguori | } |
170 | 6f97dba0 | aliguori | |
171 | 6f97dba0 | aliguori | void qemu_chr_accept_input(CharDriverState *s)
|
172 | 6f97dba0 | aliguori | { |
173 | 6f97dba0 | aliguori | if (s->chr_accept_input)
|
174 | 6f97dba0 | aliguori | s->chr_accept_input(s); |
175 | 6f97dba0 | aliguori | } |
176 | 6f97dba0 | aliguori | |
177 | 6f97dba0 | aliguori | void qemu_chr_printf(CharDriverState *s, const char *fmt, ...) |
178 | 6f97dba0 | aliguori | { |
179 | 6f97dba0 | aliguori | char buf[4096]; |
180 | 6f97dba0 | aliguori | va_list ap; |
181 | 6f97dba0 | aliguori | va_start(ap, fmt); |
182 | 6f97dba0 | aliguori | vsnprintf(buf, sizeof(buf), fmt, ap);
|
183 | 6f97dba0 | aliguori | qemu_chr_write(s, (uint8_t *)buf, strlen(buf)); |
184 | 6f97dba0 | aliguori | va_end(ap); |
185 | 6f97dba0 | aliguori | } |
186 | 6f97dba0 | aliguori | |
187 | 6f97dba0 | aliguori | void qemu_chr_send_event(CharDriverState *s, int event) |
188 | 6f97dba0 | aliguori | { |
189 | 6f97dba0 | aliguori | if (s->chr_send_event)
|
190 | 6f97dba0 | aliguori | s->chr_send_event(s, event); |
191 | 6f97dba0 | aliguori | } |
192 | 6f97dba0 | aliguori | |
193 | 6f97dba0 | aliguori | void qemu_chr_add_handlers(CharDriverState *s,
|
194 | 6f97dba0 | aliguori | IOCanRWHandler *fd_can_read, |
195 | 6f97dba0 | aliguori | IOReadHandler *fd_read, |
196 | 6f97dba0 | aliguori | IOEventHandler *fd_event, |
197 | 6f97dba0 | aliguori | void *opaque)
|
198 | 6f97dba0 | aliguori | { |
199 | 6f97dba0 | aliguori | s->chr_can_read = fd_can_read; |
200 | 6f97dba0 | aliguori | s->chr_read = fd_read; |
201 | 6f97dba0 | aliguori | s->chr_event = fd_event; |
202 | 6f97dba0 | aliguori | s->handler_opaque = opaque; |
203 | 6f97dba0 | aliguori | if (s->chr_update_read_handler)
|
204 | 6f97dba0 | aliguori | s->chr_update_read_handler(s); |
205 | 6f97dba0 | aliguori | } |
206 | 6f97dba0 | aliguori | |
207 | 6f97dba0 | aliguori | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
208 | 6f97dba0 | aliguori | { |
209 | 6f97dba0 | aliguori | return len;
|
210 | 6f97dba0 | aliguori | } |
211 | 6f97dba0 | aliguori | |
212 | 6f97dba0 | aliguori | static CharDriverState *qemu_chr_open_null(void) |
213 | 6f97dba0 | aliguori | { |
214 | 6f97dba0 | aliguori | CharDriverState *chr; |
215 | 6f97dba0 | aliguori | |
216 | 6f97dba0 | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
217 | 6f97dba0 | aliguori | chr->chr_write = null_chr_write; |
218 | 6f97dba0 | aliguori | return chr;
|
219 | 6f97dba0 | aliguori | } |
220 | 6f97dba0 | aliguori | |
221 | 6f97dba0 | aliguori | /* MUX driver for serial I/O splitting */
|
222 | 6f97dba0 | aliguori | static int term_timestamps; |
223 | 6f97dba0 | aliguori | static int64_t term_timestamps_start;
|
224 | 6f97dba0 | aliguori | #define MAX_MUX 4 |
225 | 6f97dba0 | aliguori | #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */ |
226 | 6f97dba0 | aliguori | #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1) |
227 | 6f97dba0 | aliguori | typedef struct { |
228 | 6f97dba0 | aliguori | IOCanRWHandler *chr_can_read[MAX_MUX]; |
229 | 6f97dba0 | aliguori | IOReadHandler *chr_read[MAX_MUX]; |
230 | 6f97dba0 | aliguori | IOEventHandler *chr_event[MAX_MUX]; |
231 | 6f97dba0 | aliguori | void *ext_opaque[MAX_MUX];
|
232 | 6f97dba0 | aliguori | CharDriverState *drv; |
233 | 6f97dba0 | aliguori | int mux_cnt;
|
234 | 6f97dba0 | aliguori | int term_got_escape;
|
235 | 6f97dba0 | aliguori | int max_size;
|
236 | a80bf99f | aliguori | /* Intermediate input buffer allows to catch escape sequences even if the
|
237 | a80bf99f | aliguori | currently active device is not accepting any input - but only until it
|
238 | a80bf99f | aliguori | is full as well. */
|
239 | a80bf99f | aliguori | unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE]; |
240 | a80bf99f | aliguori | int prod[MAX_MUX];
|
241 | a80bf99f | aliguori | int cons[MAX_MUX];
|
242 | 6f97dba0 | aliguori | } MuxDriver; |
243 | 6f97dba0 | aliguori | |
244 | 6f97dba0 | aliguori | |
245 | 6f97dba0 | aliguori | static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
246 | 6f97dba0 | aliguori | { |
247 | 6f97dba0 | aliguori | MuxDriver *d = chr->opaque; |
248 | 6f97dba0 | aliguori | int ret;
|
249 | 6f97dba0 | aliguori | if (!term_timestamps) {
|
250 | 6f97dba0 | aliguori | ret = d->drv->chr_write(d->drv, buf, len); |
251 | 6f97dba0 | aliguori | } else {
|
252 | 6f97dba0 | aliguori | int i;
|
253 | 6f97dba0 | aliguori | |
254 | 6f97dba0 | aliguori | ret = 0;
|
255 | 6f97dba0 | aliguori | for(i = 0; i < len; i++) { |
256 | 6f97dba0 | aliguori | ret += d->drv->chr_write(d->drv, buf+i, 1);
|
257 | 6f97dba0 | aliguori | if (buf[i] == '\n') { |
258 | 6f97dba0 | aliguori | char buf1[64]; |
259 | 6f97dba0 | aliguori | int64_t ti; |
260 | 6f97dba0 | aliguori | int secs;
|
261 | 6f97dba0 | aliguori | |
262 | 6f97dba0 | aliguori | ti = qemu_get_clock(rt_clock); |
263 | 6f97dba0 | aliguori | if (term_timestamps_start == -1) |
264 | 6f97dba0 | aliguori | term_timestamps_start = ti; |
265 | 6f97dba0 | aliguori | ti -= term_timestamps_start; |
266 | a4bb1db8 | aliguori | secs = ti / 1000;
|
267 | 6f97dba0 | aliguori | snprintf(buf1, sizeof(buf1),
|
268 | 6f97dba0 | aliguori | "[%02d:%02d:%02d.%03d] ",
|
269 | 6f97dba0 | aliguori | secs / 3600,
|
270 | 6f97dba0 | aliguori | (secs / 60) % 60, |
271 | 6f97dba0 | aliguori | secs % 60,
|
272 | a4bb1db8 | aliguori | (int)(ti % 1000)); |
273 | 6f97dba0 | aliguori | d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1)); |
274 | 6f97dba0 | aliguori | } |
275 | 6f97dba0 | aliguori | } |
276 | 6f97dba0 | aliguori | } |
277 | 6f97dba0 | aliguori | return ret;
|
278 | 6f97dba0 | aliguori | } |
279 | 6f97dba0 | aliguori | |
280 | 6f97dba0 | aliguori | static const char * const mux_help[] = { |
281 | 6f97dba0 | aliguori | "% h print this help\n\r",
|
282 | 6f97dba0 | aliguori | "% x exit emulator\n\r",
|
283 | 6f97dba0 | aliguori | "% s save disk data back to file (if -snapshot)\n\r",
|
284 | 6f97dba0 | aliguori | "% t toggle console timestamps\n\r"
|
285 | 6f97dba0 | aliguori | "% b send break (magic sysrq)\n\r",
|
286 | 6f97dba0 | aliguori | "% c switch between console and monitor\n\r",
|
287 | 6f97dba0 | aliguori | "% % sends %\n\r",
|
288 | 6f97dba0 | aliguori | NULL
|
289 | 6f97dba0 | aliguori | }; |
290 | 6f97dba0 | aliguori | |
291 | 6f97dba0 | aliguori | int term_escape_char = 0x01; /* ctrl-a is used for escape */ |
292 | 6f97dba0 | aliguori | static void mux_print_help(CharDriverState *chr) |
293 | 6f97dba0 | aliguori | { |
294 | 6f97dba0 | aliguori | int i, j;
|
295 | 6f97dba0 | aliguori | char ebuf[15] = "Escape-Char"; |
296 | 6f97dba0 | aliguori | char cbuf[50] = "\n\r"; |
297 | 6f97dba0 | aliguori | |
298 | 6f97dba0 | aliguori | if (term_escape_char > 0 && term_escape_char < 26) { |
299 | 6f97dba0 | aliguori | snprintf(cbuf, sizeof(cbuf), "\n\r"); |
300 | 6f97dba0 | aliguori | snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a'); |
301 | 6f97dba0 | aliguori | } else {
|
302 | 6f97dba0 | aliguori | snprintf(cbuf, sizeof(cbuf),
|
303 | 6f97dba0 | aliguori | "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
|
304 | 6f97dba0 | aliguori | term_escape_char); |
305 | 6f97dba0 | aliguori | } |
306 | 6f97dba0 | aliguori | chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf)); |
307 | 6f97dba0 | aliguori | for (i = 0; mux_help[i] != NULL; i++) { |
308 | 6f97dba0 | aliguori | for (j=0; mux_help[i][j] != '\0'; j++) { |
309 | 6f97dba0 | aliguori | if (mux_help[i][j] == '%') |
310 | 6f97dba0 | aliguori | chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf)); |
311 | 6f97dba0 | aliguori | else
|
312 | 6f97dba0 | aliguori | chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
|
313 | 6f97dba0 | aliguori | } |
314 | 6f97dba0 | aliguori | } |
315 | 6f97dba0 | aliguori | } |
316 | 6f97dba0 | aliguori | |
317 | 2724b180 | aliguori | static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event) |
318 | 2724b180 | aliguori | { |
319 | 2724b180 | aliguori | if (d->chr_event[mux_nr])
|
320 | 2724b180 | aliguori | d->chr_event[mux_nr](d->ext_opaque[mux_nr], event); |
321 | 2724b180 | aliguori | } |
322 | 2724b180 | aliguori | |
323 | 6f97dba0 | aliguori | static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch) |
324 | 6f97dba0 | aliguori | { |
325 | 6f97dba0 | aliguori | if (d->term_got_escape) {
|
326 | 6f97dba0 | aliguori | d->term_got_escape = 0;
|
327 | 6f97dba0 | aliguori | if (ch == term_escape_char)
|
328 | 6f97dba0 | aliguori | goto send_char;
|
329 | 6f97dba0 | aliguori | switch(ch) {
|
330 | 6f97dba0 | aliguori | case '?': |
331 | 6f97dba0 | aliguori | case 'h': |
332 | 6f97dba0 | aliguori | mux_print_help(chr); |
333 | 6f97dba0 | aliguori | break;
|
334 | 6f97dba0 | aliguori | case 'x': |
335 | 6f97dba0 | aliguori | { |
336 | 6f97dba0 | aliguori | const char *term = "QEMU: Terminated\n\r"; |
337 | 6f97dba0 | aliguori | chr->chr_write(chr,(uint8_t *)term,strlen(term)); |
338 | 6f97dba0 | aliguori | exit(0);
|
339 | 6f97dba0 | aliguori | break;
|
340 | 6f97dba0 | aliguori | } |
341 | 6f97dba0 | aliguori | case 's': |
342 | 6f97dba0 | aliguori | { |
343 | 6f97dba0 | aliguori | int i;
|
344 | 6f97dba0 | aliguori | for (i = 0; i < nb_drives; i++) { |
345 | 6f97dba0 | aliguori | bdrv_commit(drives_table[i].bdrv); |
346 | 6f97dba0 | aliguori | } |
347 | 6f97dba0 | aliguori | } |
348 | 6f97dba0 | aliguori | break;
|
349 | 6f97dba0 | aliguori | case 'b': |
350 | 6f97dba0 | aliguori | qemu_chr_event(chr, CHR_EVENT_BREAK); |
351 | 6f97dba0 | aliguori | break;
|
352 | 6f97dba0 | aliguori | case 'c': |
353 | 6f97dba0 | aliguori | /* Switch to the next registered device */
|
354 | 2724b180 | aliguori | mux_chr_send_event(d, chr->focus, CHR_EVENT_MUX_OUT); |
355 | 6f97dba0 | aliguori | chr->focus++; |
356 | 6f97dba0 | aliguori | if (chr->focus >= d->mux_cnt)
|
357 | 6f97dba0 | aliguori | chr->focus = 0;
|
358 | 2724b180 | aliguori | mux_chr_send_event(d, chr->focus, CHR_EVENT_MUX_IN); |
359 | 6f97dba0 | aliguori | break;
|
360 | 6f97dba0 | aliguori | case 't': |
361 | 6f97dba0 | aliguori | term_timestamps = !term_timestamps; |
362 | 6f97dba0 | aliguori | term_timestamps_start = -1;
|
363 | 6f97dba0 | aliguori | break;
|
364 | 6f97dba0 | aliguori | } |
365 | 6f97dba0 | aliguori | } else if (ch == term_escape_char) { |
366 | 6f97dba0 | aliguori | d->term_got_escape = 1;
|
367 | 6f97dba0 | aliguori | } else {
|
368 | 6f97dba0 | aliguori | send_char:
|
369 | 6f97dba0 | aliguori | return 1; |
370 | 6f97dba0 | aliguori | } |
371 | 6f97dba0 | aliguori | return 0; |
372 | 6f97dba0 | aliguori | } |
373 | 6f97dba0 | aliguori | |
374 | 6f97dba0 | aliguori | static void mux_chr_accept_input(CharDriverState *chr) |
375 | 6f97dba0 | aliguori | { |
376 | 6f97dba0 | aliguori | int m = chr->focus;
|
377 | 6f97dba0 | aliguori | MuxDriver *d = chr->opaque; |
378 | 6f97dba0 | aliguori | |
379 | a80bf99f | aliguori | while (d->prod[m] != d->cons[m] &&
|
380 | 6f97dba0 | aliguori | d->chr_can_read[m] && |
381 | 6f97dba0 | aliguori | d->chr_can_read[m](d->ext_opaque[m])) { |
382 | 6f97dba0 | aliguori | d->chr_read[m](d->ext_opaque[m], |
383 | a80bf99f | aliguori | &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
|
384 | 6f97dba0 | aliguori | } |
385 | 6f97dba0 | aliguori | } |
386 | 6f97dba0 | aliguori | |
387 | 6f97dba0 | aliguori | static int mux_chr_can_read(void *opaque) |
388 | 6f97dba0 | aliguori | { |
389 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
390 | 6f97dba0 | aliguori | MuxDriver *d = chr->opaque; |
391 | a80bf99f | aliguori | int m = chr->focus;
|
392 | 6f97dba0 | aliguori | |
393 | a80bf99f | aliguori | if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
|
394 | 6f97dba0 | aliguori | return 1; |
395 | a80bf99f | aliguori | if (d->chr_can_read[m])
|
396 | a80bf99f | aliguori | return d->chr_can_read[m](d->ext_opaque[m]);
|
397 | 6f97dba0 | aliguori | return 0; |
398 | 6f97dba0 | aliguori | } |
399 | 6f97dba0 | aliguori | |
400 | 6f97dba0 | aliguori | static void mux_chr_read(void *opaque, const uint8_t *buf, int size) |
401 | 6f97dba0 | aliguori | { |
402 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
403 | 6f97dba0 | aliguori | MuxDriver *d = chr->opaque; |
404 | 6f97dba0 | aliguori | int m = chr->focus;
|
405 | 6f97dba0 | aliguori | int i;
|
406 | 6f97dba0 | aliguori | |
407 | 6f97dba0 | aliguori | mux_chr_accept_input (opaque); |
408 | 6f97dba0 | aliguori | |
409 | 6f97dba0 | aliguori | for(i = 0; i < size; i++) |
410 | 6f97dba0 | aliguori | if (mux_proc_byte(chr, d, buf[i])) {
|
411 | a80bf99f | aliguori | if (d->prod[m] == d->cons[m] &&
|
412 | 6f97dba0 | aliguori | d->chr_can_read[m] && |
413 | 6f97dba0 | aliguori | d->chr_can_read[m](d->ext_opaque[m])) |
414 | 6f97dba0 | aliguori | d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
|
415 | 6f97dba0 | aliguori | else
|
416 | a80bf99f | aliguori | d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i]; |
417 | 6f97dba0 | aliguori | } |
418 | 6f97dba0 | aliguori | } |
419 | 6f97dba0 | aliguori | |
420 | 6f97dba0 | aliguori | static void mux_chr_event(void *opaque, int event) |
421 | 6f97dba0 | aliguori | { |
422 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
423 | 6f97dba0 | aliguori | MuxDriver *d = chr->opaque; |
424 | 6f97dba0 | aliguori | int i;
|
425 | 6f97dba0 | aliguori | |
426 | 6f97dba0 | aliguori | /* Send the event to all registered listeners */
|
427 | 6f97dba0 | aliguori | for (i = 0; i < d->mux_cnt; i++) |
428 | 2724b180 | aliguori | mux_chr_send_event(d, i, event); |
429 | 6f97dba0 | aliguori | } |
430 | 6f97dba0 | aliguori | |
431 | 6f97dba0 | aliguori | static void mux_chr_update_read_handler(CharDriverState *chr) |
432 | 6f97dba0 | aliguori | { |
433 | 6f97dba0 | aliguori | MuxDriver *d = chr->opaque; |
434 | 6f97dba0 | aliguori | |
435 | 6f97dba0 | aliguori | if (d->mux_cnt >= MAX_MUX) {
|
436 | 6f97dba0 | aliguori | fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
|
437 | 6f97dba0 | aliguori | return;
|
438 | 6f97dba0 | aliguori | } |
439 | 6f97dba0 | aliguori | d->ext_opaque[d->mux_cnt] = chr->handler_opaque; |
440 | 6f97dba0 | aliguori | d->chr_can_read[d->mux_cnt] = chr->chr_can_read; |
441 | 6f97dba0 | aliguori | d->chr_read[d->mux_cnt] = chr->chr_read; |
442 | 6f97dba0 | aliguori | d->chr_event[d->mux_cnt] = chr->chr_event; |
443 | 6f97dba0 | aliguori | /* Fix up the real driver with mux routines */
|
444 | 6f97dba0 | aliguori | if (d->mux_cnt == 0) { |
445 | 6f97dba0 | aliguori | qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read, |
446 | 6f97dba0 | aliguori | mux_chr_event, chr); |
447 | 6f97dba0 | aliguori | } |
448 | 6f97dba0 | aliguori | chr->focus = d->mux_cnt; |
449 | 6f97dba0 | aliguori | d->mux_cnt++; |
450 | 6f97dba0 | aliguori | } |
451 | 6f97dba0 | aliguori | |
452 | 6f97dba0 | aliguori | static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
|
453 | 6f97dba0 | aliguori | { |
454 | 6f97dba0 | aliguori | CharDriverState *chr; |
455 | 6f97dba0 | aliguori | MuxDriver *d; |
456 | 6f97dba0 | aliguori | |
457 | 6f97dba0 | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
458 | 6f97dba0 | aliguori | d = qemu_mallocz(sizeof(MuxDriver));
|
459 | 6f97dba0 | aliguori | |
460 | 6f97dba0 | aliguori | chr->opaque = d; |
461 | 6f97dba0 | aliguori | d->drv = drv; |
462 | 6f97dba0 | aliguori | chr->focus = -1;
|
463 | 6f97dba0 | aliguori | chr->chr_write = mux_chr_write; |
464 | 6f97dba0 | aliguori | chr->chr_update_read_handler = mux_chr_update_read_handler; |
465 | 6f97dba0 | aliguori | chr->chr_accept_input = mux_chr_accept_input; |
466 | 6f97dba0 | aliguori | return chr;
|
467 | 6f97dba0 | aliguori | } |
468 | 6f97dba0 | aliguori | |
469 | 6f97dba0 | aliguori | |
470 | 6f97dba0 | aliguori | #ifdef _WIN32
|
471 | d247d25f | aliguori | int send_all(int fd, const void *buf, int len1) |
472 | 6f97dba0 | aliguori | { |
473 | 6f97dba0 | aliguori | int ret, len;
|
474 | 6f97dba0 | aliguori | |
475 | 6f97dba0 | aliguori | len = len1; |
476 | 6f97dba0 | aliguori | while (len > 0) { |
477 | 6f97dba0 | aliguori | ret = send(fd, buf, len, 0);
|
478 | 6f97dba0 | aliguori | if (ret < 0) { |
479 | 6f97dba0 | aliguori | errno = WSAGetLastError(); |
480 | 6f97dba0 | aliguori | if (errno != WSAEWOULDBLOCK) {
|
481 | 6f97dba0 | aliguori | return -1; |
482 | 6f97dba0 | aliguori | } |
483 | 6f97dba0 | aliguori | } else if (ret == 0) { |
484 | 6f97dba0 | aliguori | break;
|
485 | 6f97dba0 | aliguori | } else {
|
486 | 6f97dba0 | aliguori | buf += ret; |
487 | 6f97dba0 | aliguori | len -= ret; |
488 | 6f97dba0 | aliguori | } |
489 | 6f97dba0 | aliguori | } |
490 | 6f97dba0 | aliguori | return len1 - len;
|
491 | 6f97dba0 | aliguori | } |
492 | 6f97dba0 | aliguori | |
493 | 6f97dba0 | aliguori | #else
|
494 | 6f97dba0 | aliguori | |
495 | 6f97dba0 | aliguori | static int unix_write(int fd, const uint8_t *buf, int len1) |
496 | 6f97dba0 | aliguori | { |
497 | 6f97dba0 | aliguori | int ret, len;
|
498 | 6f97dba0 | aliguori | |
499 | 6f97dba0 | aliguori | len = len1; |
500 | 6f97dba0 | aliguori | while (len > 0) { |
501 | 6f97dba0 | aliguori | ret = write(fd, buf, len); |
502 | 6f97dba0 | aliguori | if (ret < 0) { |
503 | 6f97dba0 | aliguori | if (errno != EINTR && errno != EAGAIN)
|
504 | 6f97dba0 | aliguori | return -1; |
505 | 6f97dba0 | aliguori | } else if (ret == 0) { |
506 | 6f97dba0 | aliguori | break;
|
507 | 6f97dba0 | aliguori | } else {
|
508 | 6f97dba0 | aliguori | buf += ret; |
509 | 6f97dba0 | aliguori | len -= ret; |
510 | 6f97dba0 | aliguori | } |
511 | 6f97dba0 | aliguori | } |
512 | 6f97dba0 | aliguori | return len1 - len;
|
513 | 6f97dba0 | aliguori | } |
514 | 6f97dba0 | aliguori | |
515 | d247d25f | aliguori | int send_all(int fd, const void *buf, int len1) |
516 | 6f97dba0 | aliguori | { |
517 | 6f97dba0 | aliguori | return unix_write(fd, buf, len1);
|
518 | 6f97dba0 | aliguori | } |
519 | 6f97dba0 | aliguori | #endif /* !_WIN32 */ |
520 | 6f97dba0 | aliguori | |
521 | 6f97dba0 | aliguori | #ifndef _WIN32
|
522 | 6f97dba0 | aliguori | |
523 | 6f97dba0 | aliguori | typedef struct { |
524 | 6f97dba0 | aliguori | int fd_in, fd_out;
|
525 | 6f97dba0 | aliguori | int max_size;
|
526 | 6f97dba0 | aliguori | } FDCharDriver; |
527 | 6f97dba0 | aliguori | |
528 | 6f97dba0 | aliguori | #define STDIO_MAX_CLIENTS 1 |
529 | 6f97dba0 | aliguori | static int stdio_nb_clients = 0; |
530 | 6f97dba0 | aliguori | |
531 | 6f97dba0 | aliguori | static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
532 | 6f97dba0 | aliguori | { |
533 | 6f97dba0 | aliguori | FDCharDriver *s = chr->opaque; |
534 | 6f97dba0 | aliguori | return send_all(s->fd_out, buf, len);
|
535 | 6f97dba0 | aliguori | } |
536 | 6f97dba0 | aliguori | |
537 | 6f97dba0 | aliguori | static int fd_chr_read_poll(void *opaque) |
538 | 6f97dba0 | aliguori | { |
539 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
540 | 6f97dba0 | aliguori | FDCharDriver *s = chr->opaque; |
541 | 6f97dba0 | aliguori | |
542 | 6f97dba0 | aliguori | s->max_size = qemu_chr_can_read(chr); |
543 | 6f97dba0 | aliguori | return s->max_size;
|
544 | 6f97dba0 | aliguori | } |
545 | 6f97dba0 | aliguori | |
546 | 6f97dba0 | aliguori | static void fd_chr_read(void *opaque) |
547 | 6f97dba0 | aliguori | { |
548 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
549 | 6f97dba0 | aliguori | FDCharDriver *s = chr->opaque; |
550 | 6f97dba0 | aliguori | int size, len;
|
551 | 6f97dba0 | aliguori | uint8_t buf[1024];
|
552 | 6f97dba0 | aliguori | |
553 | 6f97dba0 | aliguori | len = sizeof(buf);
|
554 | 6f97dba0 | aliguori | if (len > s->max_size)
|
555 | 6f97dba0 | aliguori | len = s->max_size; |
556 | 6f97dba0 | aliguori | if (len == 0) |
557 | 6f97dba0 | aliguori | return;
|
558 | 6f97dba0 | aliguori | size = read(s->fd_in, buf, len); |
559 | 6f97dba0 | aliguori | if (size == 0) { |
560 | 6f97dba0 | aliguori | /* FD has been closed. Remove it from the active list. */
|
561 | 6f97dba0 | aliguori | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
562 | 6f97dba0 | aliguori | return;
|
563 | 6f97dba0 | aliguori | } |
564 | 6f97dba0 | aliguori | if (size > 0) { |
565 | 6f97dba0 | aliguori | qemu_chr_read(chr, buf, size); |
566 | 6f97dba0 | aliguori | } |
567 | 6f97dba0 | aliguori | } |
568 | 6f97dba0 | aliguori | |
569 | 6f97dba0 | aliguori | static void fd_chr_update_read_handler(CharDriverState *chr) |
570 | 6f97dba0 | aliguori | { |
571 | 6f97dba0 | aliguori | FDCharDriver *s = chr->opaque; |
572 | 6f97dba0 | aliguori | |
573 | 6f97dba0 | aliguori | if (s->fd_in >= 0) { |
574 | 6f97dba0 | aliguori | if (nographic && s->fd_in == 0) { |
575 | 6f97dba0 | aliguori | } else {
|
576 | 6f97dba0 | aliguori | qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll, |
577 | 6f97dba0 | aliguori | fd_chr_read, NULL, chr);
|
578 | 6f97dba0 | aliguori | } |
579 | 6f97dba0 | aliguori | } |
580 | 6f97dba0 | aliguori | } |
581 | 6f97dba0 | aliguori | |
582 | 6f97dba0 | aliguori | static void fd_chr_close(struct CharDriverState *chr) |
583 | 6f97dba0 | aliguori | { |
584 | 6f97dba0 | aliguori | FDCharDriver *s = chr->opaque; |
585 | 6f97dba0 | aliguori | |
586 | 6f97dba0 | aliguori | if (s->fd_in >= 0) { |
587 | 6f97dba0 | aliguori | if (nographic && s->fd_in == 0) { |
588 | 6f97dba0 | aliguori | } else {
|
589 | 6f97dba0 | aliguori | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
590 | 6f97dba0 | aliguori | } |
591 | 6f97dba0 | aliguori | } |
592 | 6f97dba0 | aliguori | |
593 | 6f97dba0 | aliguori | qemu_free(s); |
594 | 6f97dba0 | aliguori | } |
595 | 6f97dba0 | aliguori | |
596 | 6f97dba0 | aliguori | /* open a character device to a unix fd */
|
597 | 6f97dba0 | aliguori | static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) |
598 | 6f97dba0 | aliguori | { |
599 | 6f97dba0 | aliguori | CharDriverState *chr; |
600 | 6f97dba0 | aliguori | FDCharDriver *s; |
601 | 6f97dba0 | aliguori | |
602 | 6f97dba0 | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
603 | 6f97dba0 | aliguori | s = qemu_mallocz(sizeof(FDCharDriver));
|
604 | 6f97dba0 | aliguori | s->fd_in = fd_in; |
605 | 6f97dba0 | aliguori | s->fd_out = fd_out; |
606 | 6f97dba0 | aliguori | chr->opaque = s; |
607 | 6f97dba0 | aliguori | chr->chr_write = fd_chr_write; |
608 | 6f97dba0 | aliguori | chr->chr_update_read_handler = fd_chr_update_read_handler; |
609 | 6f97dba0 | aliguori | chr->chr_close = fd_chr_close; |
610 | 6f97dba0 | aliguori | |
611 | 6f97dba0 | aliguori | qemu_chr_reset(chr); |
612 | 6f97dba0 | aliguori | |
613 | 6f97dba0 | aliguori | return chr;
|
614 | 6f97dba0 | aliguori | } |
615 | 6f97dba0 | aliguori | |
616 | 6f97dba0 | aliguori | static CharDriverState *qemu_chr_open_file_out(const char *file_out) |
617 | 6f97dba0 | aliguori | { |
618 | 6f97dba0 | aliguori | int fd_out;
|
619 | 6f97dba0 | aliguori | |
620 | 6f97dba0 | aliguori | TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
|
621 | 6f97dba0 | aliguori | if (fd_out < 0) |
622 | 6f97dba0 | aliguori | return NULL; |
623 | 6f97dba0 | aliguori | return qemu_chr_open_fd(-1, fd_out); |
624 | 6f97dba0 | aliguori | } |
625 | 6f97dba0 | aliguori | |
626 | 6f97dba0 | aliguori | static CharDriverState *qemu_chr_open_pipe(const char *filename) |
627 | 6f97dba0 | aliguori | { |
628 | 6f97dba0 | aliguori | int fd_in, fd_out;
|
629 | 6f97dba0 | aliguori | char filename_in[256], filename_out[256]; |
630 | 6f97dba0 | aliguori | |
631 | 6f97dba0 | aliguori | snprintf(filename_in, 256, "%s.in", filename); |
632 | 6f97dba0 | aliguori | snprintf(filename_out, 256, "%s.out", filename); |
633 | 6f97dba0 | aliguori | TFR(fd_in = open(filename_in, O_RDWR | O_BINARY)); |
634 | 6f97dba0 | aliguori | TFR(fd_out = open(filename_out, O_RDWR | O_BINARY)); |
635 | 6f97dba0 | aliguori | if (fd_in < 0 || fd_out < 0) { |
636 | 6f97dba0 | aliguori | if (fd_in >= 0) |
637 | 6f97dba0 | aliguori | close(fd_in); |
638 | 6f97dba0 | aliguori | if (fd_out >= 0) |
639 | 6f97dba0 | aliguori | close(fd_out); |
640 | 6f97dba0 | aliguori | TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY)); |
641 | 6f97dba0 | aliguori | if (fd_in < 0) |
642 | 6f97dba0 | aliguori | return NULL; |
643 | 6f97dba0 | aliguori | } |
644 | 6f97dba0 | aliguori | return qemu_chr_open_fd(fd_in, fd_out);
|
645 | 6f97dba0 | aliguori | } |
646 | 6f97dba0 | aliguori | |
647 | 6f97dba0 | aliguori | |
648 | 6f97dba0 | aliguori | /* for STDIO, we handle the case where several clients use it
|
649 | 6f97dba0 | aliguori | (nographic mode) */
|
650 | 6f97dba0 | aliguori | |
651 | 6f97dba0 | aliguori | #define TERM_FIFO_MAX_SIZE 1 |
652 | 6f97dba0 | aliguori | |
653 | 6f97dba0 | aliguori | static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
|
654 | 6f97dba0 | aliguori | static int term_fifo_size; |
655 | 6f97dba0 | aliguori | |
656 | 6f97dba0 | aliguori | static int stdio_read_poll(void *opaque) |
657 | 6f97dba0 | aliguori | { |
658 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
659 | 6f97dba0 | aliguori | |
660 | 6f97dba0 | aliguori | /* try to flush the queue if needed */
|
661 | 6f97dba0 | aliguori | if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) { |
662 | 6f97dba0 | aliguori | qemu_chr_read(chr, term_fifo, 1);
|
663 | 6f97dba0 | aliguori | term_fifo_size = 0;
|
664 | 6f97dba0 | aliguori | } |
665 | 6f97dba0 | aliguori | /* see if we can absorb more chars */
|
666 | 6f97dba0 | aliguori | if (term_fifo_size == 0) |
667 | 6f97dba0 | aliguori | return 1; |
668 | 6f97dba0 | aliguori | else
|
669 | 6f97dba0 | aliguori | return 0; |
670 | 6f97dba0 | aliguori | } |
671 | 6f97dba0 | aliguori | |
672 | 6f97dba0 | aliguori | static void stdio_read(void *opaque) |
673 | 6f97dba0 | aliguori | { |
674 | 6f97dba0 | aliguori | int size;
|
675 | 6f97dba0 | aliguori | uint8_t buf[1];
|
676 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
677 | 6f97dba0 | aliguori | |
678 | 6f97dba0 | aliguori | size = read(0, buf, 1); |
679 | 6f97dba0 | aliguori | if (size == 0) { |
680 | 6f97dba0 | aliguori | /* stdin has been closed. Remove it from the active list. */
|
681 | 6f97dba0 | aliguori | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
682 | 6f97dba0 | aliguori | return;
|
683 | 6f97dba0 | aliguori | } |
684 | 6f97dba0 | aliguori | if (size > 0) { |
685 | 6f97dba0 | aliguori | if (qemu_chr_can_read(chr) > 0) { |
686 | 6f97dba0 | aliguori | qemu_chr_read(chr, buf, 1);
|
687 | 6f97dba0 | aliguori | } else if (term_fifo_size == 0) { |
688 | 6f97dba0 | aliguori | term_fifo[term_fifo_size++] = buf[0];
|
689 | 6f97dba0 | aliguori | } |
690 | 6f97dba0 | aliguori | } |
691 | 6f97dba0 | aliguori | } |
692 | 6f97dba0 | aliguori | |
693 | 6f97dba0 | aliguori | /* init terminal so that we can grab keys */
|
694 | 6f97dba0 | aliguori | static struct termios oldtty; |
695 | 6f97dba0 | aliguori | static int old_fd0_flags; |
696 | 6f97dba0 | aliguori | static int term_atexit_done; |
697 | 6f97dba0 | aliguori | |
698 | 6f97dba0 | aliguori | static void term_exit(void) |
699 | 6f97dba0 | aliguori | { |
700 | 6f97dba0 | aliguori | tcsetattr (0, TCSANOW, &oldtty);
|
701 | 6f97dba0 | aliguori | fcntl(0, F_SETFL, old_fd0_flags);
|
702 | 6f97dba0 | aliguori | } |
703 | 6f97dba0 | aliguori | |
704 | 6f97dba0 | aliguori | static void term_init(void) |
705 | 6f97dba0 | aliguori | { |
706 | 6f97dba0 | aliguori | struct termios tty;
|
707 | 6f97dba0 | aliguori | |
708 | 6f97dba0 | aliguori | tcgetattr (0, &tty);
|
709 | 6f97dba0 | aliguori | oldtty = tty; |
710 | 6f97dba0 | aliguori | old_fd0_flags = fcntl(0, F_GETFL);
|
711 | 6f97dba0 | aliguori | |
712 | 6f97dba0 | aliguori | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
713 | 6f97dba0 | aliguori | |INLCR|IGNCR|ICRNL|IXON); |
714 | 6f97dba0 | aliguori | tty.c_oflag |= OPOST; |
715 | 6f97dba0 | aliguori | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
716 | 6f97dba0 | aliguori | /* if graphical mode, we allow Ctrl-C handling */
|
717 | 6f97dba0 | aliguori | if (nographic)
|
718 | 6f97dba0 | aliguori | tty.c_lflag &= ~ISIG; |
719 | 6f97dba0 | aliguori | tty.c_cflag &= ~(CSIZE|PARENB); |
720 | 6f97dba0 | aliguori | tty.c_cflag |= CS8; |
721 | 6f97dba0 | aliguori | tty.c_cc[VMIN] = 1;
|
722 | 6f97dba0 | aliguori | tty.c_cc[VTIME] = 0;
|
723 | 6f97dba0 | aliguori | |
724 | 6f97dba0 | aliguori | tcsetattr (0, TCSANOW, &tty);
|
725 | 6f97dba0 | aliguori | |
726 | 6f97dba0 | aliguori | if (!term_atexit_done++)
|
727 | 6f97dba0 | aliguori | atexit(term_exit); |
728 | 6f97dba0 | aliguori | |
729 | 6f97dba0 | aliguori | fcntl(0, F_SETFL, O_NONBLOCK);
|
730 | 6f97dba0 | aliguori | } |
731 | 6f97dba0 | aliguori | |
732 | 6f97dba0 | aliguori | static void qemu_chr_close_stdio(struct CharDriverState *chr) |
733 | 6f97dba0 | aliguori | { |
734 | 6f97dba0 | aliguori | term_exit(); |
735 | 6f97dba0 | aliguori | stdio_nb_clients--; |
736 | 6f97dba0 | aliguori | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
737 | 6f97dba0 | aliguori | fd_chr_close(chr); |
738 | 6f97dba0 | aliguori | } |
739 | 6f97dba0 | aliguori | |
740 | 6f97dba0 | aliguori | static CharDriverState *qemu_chr_open_stdio(void) |
741 | 6f97dba0 | aliguori | { |
742 | 6f97dba0 | aliguori | CharDriverState *chr; |
743 | 6f97dba0 | aliguori | |
744 | 6f97dba0 | aliguori | if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
|
745 | 6f97dba0 | aliguori | return NULL; |
746 | 6f97dba0 | aliguori | chr = qemu_chr_open_fd(0, 1); |
747 | 6f97dba0 | aliguori | chr->chr_close = qemu_chr_close_stdio; |
748 | 6f97dba0 | aliguori | qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr); |
749 | 6f97dba0 | aliguori | stdio_nb_clients++; |
750 | 6f97dba0 | aliguori | term_init(); |
751 | 6f97dba0 | aliguori | |
752 | 6f97dba0 | aliguori | return chr;
|
753 | 6f97dba0 | aliguori | } |
754 | 6f97dba0 | aliguori | |
755 | 6f97dba0 | aliguori | #ifdef __sun__
|
756 | 6f97dba0 | aliguori | /* Once Solaris has openpty(), this is going to be removed. */
|
757 | 6f97dba0 | aliguori | int openpty(int *amaster, int *aslave, char *name, |
758 | 6f97dba0 | aliguori | struct termios *termp, struct winsize *winp) |
759 | 6f97dba0 | aliguori | { |
760 | 6f97dba0 | aliguori | const char *slave; |
761 | 6f97dba0 | aliguori | int mfd = -1, sfd = -1; |
762 | 6f97dba0 | aliguori | |
763 | 6f97dba0 | aliguori | *amaster = *aslave = -1;
|
764 | 6f97dba0 | aliguori | |
765 | 6f97dba0 | aliguori | mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
|
766 | 6f97dba0 | aliguori | if (mfd < 0) |
767 | 6f97dba0 | aliguori | goto err;
|
768 | 6f97dba0 | aliguori | |
769 | 6f97dba0 | aliguori | if (grantpt(mfd) == -1 || unlockpt(mfd) == -1) |
770 | 6f97dba0 | aliguori | goto err;
|
771 | 6f97dba0 | aliguori | |
772 | 6f97dba0 | aliguori | if ((slave = ptsname(mfd)) == NULL) |
773 | 6f97dba0 | aliguori | goto err;
|
774 | 6f97dba0 | aliguori | |
775 | 6f97dba0 | aliguori | if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1) |
776 | 6f97dba0 | aliguori | goto err;
|
777 | 6f97dba0 | aliguori | |
778 | 6f97dba0 | aliguori | if (ioctl(sfd, I_PUSH, "ptem") == -1 || |
779 | 6f97dba0 | aliguori | (termp != NULL && tcgetattr(sfd, termp) < 0)) |
780 | 6f97dba0 | aliguori | goto err;
|
781 | 6f97dba0 | aliguori | |
782 | 6f97dba0 | aliguori | if (amaster)
|
783 | 6f97dba0 | aliguori | *amaster = mfd; |
784 | 6f97dba0 | aliguori | if (aslave)
|
785 | 6f97dba0 | aliguori | *aslave = sfd; |
786 | 6f97dba0 | aliguori | if (winp)
|
787 | 6f97dba0 | aliguori | ioctl(sfd, TIOCSWINSZ, winp); |
788 | 6f97dba0 | aliguori | |
789 | 6f97dba0 | aliguori | return 0; |
790 | 6f97dba0 | aliguori | |
791 | 6f97dba0 | aliguori | err:
|
792 | 6f97dba0 | aliguori | if (sfd != -1) |
793 | 6f97dba0 | aliguori | close(sfd); |
794 | 6f97dba0 | aliguori | close(mfd); |
795 | 6f97dba0 | aliguori | return -1; |
796 | 6f97dba0 | aliguori | } |
797 | 6f97dba0 | aliguori | |
798 | 6f97dba0 | aliguori | void cfmakeraw (struct termios *termios_p) |
799 | 6f97dba0 | aliguori | { |
800 | 6f97dba0 | aliguori | termios_p->c_iflag &= |
801 | 6f97dba0 | aliguori | ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); |
802 | 6f97dba0 | aliguori | termios_p->c_oflag &= ~OPOST; |
803 | 6f97dba0 | aliguori | termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); |
804 | 6f97dba0 | aliguori | termios_p->c_cflag &= ~(CSIZE|PARENB); |
805 | 6f97dba0 | aliguori | termios_p->c_cflag |= CS8; |
806 | 6f97dba0 | aliguori | |
807 | 6f97dba0 | aliguori | termios_p->c_cc[VMIN] = 0;
|
808 | 6f97dba0 | aliguori | termios_p->c_cc[VTIME] = 0;
|
809 | 6f97dba0 | aliguori | } |
810 | 6f97dba0 | aliguori | #endif
|
811 | 6f97dba0 | aliguori | |
812 | 6f97dba0 | aliguori | #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
|
813 | c5e97233 | blueswir1 | || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) |
814 | 6f97dba0 | aliguori | |
815 | 6f97dba0 | aliguori | typedef struct { |
816 | 6f97dba0 | aliguori | int fd;
|
817 | 6f97dba0 | aliguori | int connected;
|
818 | 6f97dba0 | aliguori | int polling;
|
819 | 6f97dba0 | aliguori | int read_bytes;
|
820 | 6f97dba0 | aliguori | QEMUTimer *timer; |
821 | 6f97dba0 | aliguori | } PtyCharDriver; |
822 | 6f97dba0 | aliguori | |
823 | 6f97dba0 | aliguori | static void pty_chr_update_read_handler(CharDriverState *chr); |
824 | 6f97dba0 | aliguori | static void pty_chr_state(CharDriverState *chr, int connected); |
825 | 6f97dba0 | aliguori | |
826 | 6f97dba0 | aliguori | static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
827 | 6f97dba0 | aliguori | { |
828 | 6f97dba0 | aliguori | PtyCharDriver *s = chr->opaque; |
829 | 6f97dba0 | aliguori | |
830 | 6f97dba0 | aliguori | if (!s->connected) {
|
831 | 6f97dba0 | aliguori | /* guest sends data, check for (re-)connect */
|
832 | 6f97dba0 | aliguori | pty_chr_update_read_handler(chr); |
833 | 6f97dba0 | aliguori | return 0; |
834 | 6f97dba0 | aliguori | } |
835 | 6f97dba0 | aliguori | return send_all(s->fd, buf, len);
|
836 | 6f97dba0 | aliguori | } |
837 | 6f97dba0 | aliguori | |
838 | 6f97dba0 | aliguori | static int pty_chr_read_poll(void *opaque) |
839 | 6f97dba0 | aliguori | { |
840 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
841 | 6f97dba0 | aliguori | PtyCharDriver *s = chr->opaque; |
842 | 6f97dba0 | aliguori | |
843 | 6f97dba0 | aliguori | s->read_bytes = qemu_chr_can_read(chr); |
844 | 6f97dba0 | aliguori | return s->read_bytes;
|
845 | 6f97dba0 | aliguori | } |
846 | 6f97dba0 | aliguori | |
847 | 6f97dba0 | aliguori | static void pty_chr_read(void *opaque) |
848 | 6f97dba0 | aliguori | { |
849 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
850 | 6f97dba0 | aliguori | PtyCharDriver *s = chr->opaque; |
851 | 6f97dba0 | aliguori | int size, len;
|
852 | 6f97dba0 | aliguori | uint8_t buf[1024];
|
853 | 6f97dba0 | aliguori | |
854 | 6f97dba0 | aliguori | len = sizeof(buf);
|
855 | 6f97dba0 | aliguori | if (len > s->read_bytes)
|
856 | 6f97dba0 | aliguori | len = s->read_bytes; |
857 | 6f97dba0 | aliguori | if (len == 0) |
858 | 6f97dba0 | aliguori | return;
|
859 | 6f97dba0 | aliguori | size = read(s->fd, buf, len); |
860 | 6f97dba0 | aliguori | if ((size == -1 && errno == EIO) || |
861 | 6f97dba0 | aliguori | (size == 0)) {
|
862 | 6f97dba0 | aliguori | pty_chr_state(chr, 0);
|
863 | 6f97dba0 | aliguori | return;
|
864 | 6f97dba0 | aliguori | } |
865 | 6f97dba0 | aliguori | if (size > 0) { |
866 | 6f97dba0 | aliguori | pty_chr_state(chr, 1);
|
867 | 6f97dba0 | aliguori | qemu_chr_read(chr, buf, size); |
868 | 6f97dba0 | aliguori | } |
869 | 6f97dba0 | aliguori | } |
870 | 6f97dba0 | aliguori | |
871 | 6f97dba0 | aliguori | static void pty_chr_update_read_handler(CharDriverState *chr) |
872 | 6f97dba0 | aliguori | { |
873 | 6f97dba0 | aliguori | PtyCharDriver *s = chr->opaque; |
874 | 6f97dba0 | aliguori | |
875 | 6f97dba0 | aliguori | qemu_set_fd_handler2(s->fd, pty_chr_read_poll, |
876 | 6f97dba0 | aliguori | pty_chr_read, NULL, chr);
|
877 | 6f97dba0 | aliguori | s->polling = 1;
|
878 | 6f97dba0 | aliguori | /*
|
879 | 6f97dba0 | aliguori | * Short timeout here: just need wait long enougth that qemu makes
|
880 | 6f97dba0 | aliguori | * it through the poll loop once. When reconnected we want a
|
881 | 6f97dba0 | aliguori | * short timeout so we notice it almost instantly. Otherwise
|
882 | 6f97dba0 | aliguori | * read() gives us -EIO instantly, making pty_chr_state() reset the
|
883 | 6f97dba0 | aliguori | * timeout to the normal (much longer) poll interval before the
|
884 | 6f97dba0 | aliguori | * timer triggers.
|
885 | 6f97dba0 | aliguori | */
|
886 | 6f97dba0 | aliguori | qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
|
887 | 6f97dba0 | aliguori | } |
888 | 6f97dba0 | aliguori | |
889 | 6f97dba0 | aliguori | static void pty_chr_state(CharDriverState *chr, int connected) |
890 | 6f97dba0 | aliguori | { |
891 | 6f97dba0 | aliguori | PtyCharDriver *s = chr->opaque; |
892 | 6f97dba0 | aliguori | |
893 | 6f97dba0 | aliguori | if (!connected) {
|
894 | 6f97dba0 | aliguori | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
895 | 6f97dba0 | aliguori | s->connected = 0;
|
896 | 6f97dba0 | aliguori | s->polling = 0;
|
897 | 6f97dba0 | aliguori | /* (re-)connect poll interval for idle guests: once per second.
|
898 | 6f97dba0 | aliguori | * We check more frequently in case the guests sends data to
|
899 | 6f97dba0 | aliguori | * the virtual device linked to our pty. */
|
900 | 6f97dba0 | aliguori | qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
|
901 | 6f97dba0 | aliguori | } else {
|
902 | 6f97dba0 | aliguori | if (!s->connected)
|
903 | 6f97dba0 | aliguori | qemu_chr_reset(chr); |
904 | 6f97dba0 | aliguori | s->connected = 1;
|
905 | 6f97dba0 | aliguori | } |
906 | 6f97dba0 | aliguori | } |
907 | 6f97dba0 | aliguori | |
908 | 6f97dba0 | aliguori | static void pty_chr_timer(void *opaque) |
909 | 6f97dba0 | aliguori | { |
910 | 6f97dba0 | aliguori | struct CharDriverState *chr = opaque;
|
911 | 6f97dba0 | aliguori | PtyCharDriver *s = chr->opaque; |
912 | 6f97dba0 | aliguori | |
913 | 6f97dba0 | aliguori | if (s->connected)
|
914 | 6f97dba0 | aliguori | return;
|
915 | 6f97dba0 | aliguori | if (s->polling) {
|
916 | 6f97dba0 | aliguori | /* If we arrive here without polling being cleared due
|
917 | 6f97dba0 | aliguori | * read returning -EIO, then we are (re-)connected */
|
918 | 6f97dba0 | aliguori | pty_chr_state(chr, 1);
|
919 | 6f97dba0 | aliguori | return;
|
920 | 6f97dba0 | aliguori | } |
921 | 6f97dba0 | aliguori | |
922 | 6f97dba0 | aliguori | /* Next poll ... */
|
923 | 6f97dba0 | aliguori | pty_chr_update_read_handler(chr); |
924 | 6f97dba0 | aliguori | } |
925 | 6f97dba0 | aliguori | |
926 | 6f97dba0 | aliguori | static void pty_chr_close(struct CharDriverState *chr) |
927 | 6f97dba0 | aliguori | { |
928 | 6f97dba0 | aliguori | PtyCharDriver *s = chr->opaque; |
929 | 6f97dba0 | aliguori | |
930 | 6f97dba0 | aliguori | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
931 | 6f97dba0 | aliguori | close(s->fd); |
932 | 6f97dba0 | aliguori | qemu_free(s); |
933 | 6f97dba0 | aliguori | } |
934 | 6f97dba0 | aliguori | |
935 | 6f97dba0 | aliguori | static CharDriverState *qemu_chr_open_pty(void) |
936 | 6f97dba0 | aliguori | { |
937 | 6f97dba0 | aliguori | CharDriverState *chr; |
938 | 6f97dba0 | aliguori | PtyCharDriver *s; |
939 | 6f97dba0 | aliguori | struct termios tty;
|
940 | 6f97dba0 | aliguori | int slave_fd, len;
|
941 | c5e97233 | blueswir1 | #if defined(__OpenBSD__) || defined(__DragonFly__)
|
942 | 6f97dba0 | aliguori | char pty_name[PATH_MAX];
|
943 | 6f97dba0 | aliguori | #define q_ptsname(x) pty_name
|
944 | 6f97dba0 | aliguori | #else
|
945 | 6f97dba0 | aliguori | char *pty_name = NULL; |
946 | 6f97dba0 | aliguori | #define q_ptsname(x) ptsname(x)
|
947 | 6f97dba0 | aliguori | #endif
|
948 | 6f97dba0 | aliguori | |
949 | 6f97dba0 | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
950 | 6f97dba0 | aliguori | s = qemu_mallocz(sizeof(PtyCharDriver));
|
951 | 6f97dba0 | aliguori | |
952 | 6f97dba0 | aliguori | if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) { |
953 | 6f97dba0 | aliguori | return NULL; |
954 | 6f97dba0 | aliguori | } |
955 | 6f97dba0 | aliguori | |
956 | 6f97dba0 | aliguori | /* Set raw attributes on the pty. */
|
957 | c3b972c3 | aliguori | tcgetattr(slave_fd, &tty); |
958 | 6f97dba0 | aliguori | cfmakeraw(&tty); |
959 | 6f97dba0 | aliguori | tcsetattr(slave_fd, TCSAFLUSH, &tty); |
960 | 6f97dba0 | aliguori | close(slave_fd); |
961 | 6f97dba0 | aliguori | |
962 | 6f97dba0 | aliguori | len = strlen(q_ptsname(s->fd)) + 5;
|
963 | 6f97dba0 | aliguori | chr->filename = qemu_malloc(len); |
964 | 6f97dba0 | aliguori | snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
|
965 | 6f97dba0 | aliguori | fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
|
966 | 6f97dba0 | aliguori | |
967 | 6f97dba0 | aliguori | chr->opaque = s; |
968 | 6f97dba0 | aliguori | chr->chr_write = pty_chr_write; |
969 | 6f97dba0 | aliguori | chr->chr_update_read_handler = pty_chr_update_read_handler; |
970 | 6f97dba0 | aliguori | chr->chr_close = pty_chr_close; |
971 | 6f97dba0 | aliguori | |
972 | 6f97dba0 | aliguori | s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr); |
973 | 6f97dba0 | aliguori | |
974 | 6f97dba0 | aliguori | return chr;
|
975 | 6f97dba0 | aliguori | } |
976 | 6f97dba0 | aliguori | |
977 | 6f97dba0 | aliguori | static void tty_serial_init(int fd, int speed, |
978 | 6f97dba0 | aliguori | int parity, int data_bits, int stop_bits) |
979 | 6f97dba0 | aliguori | { |
980 | 6f97dba0 | aliguori | struct termios tty;
|
981 | 6f97dba0 | aliguori | speed_t spd; |
982 | 6f97dba0 | aliguori | |
983 | 6f97dba0 | aliguori | #if 0
|
984 | 6f97dba0 | aliguori | printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
|
985 | 6f97dba0 | aliguori | speed, parity, data_bits, stop_bits);
|
986 | 6f97dba0 | aliguori | #endif
|
987 | 6f97dba0 | aliguori | tcgetattr (fd, &tty); |
988 | 6f97dba0 | aliguori | |
989 | 6f97dba0 | aliguori | #define MARGIN 1.1 |
990 | 6f97dba0 | aliguori | if (speed <= 50 * MARGIN) |
991 | 6f97dba0 | aliguori | spd = B50; |
992 | 6f97dba0 | aliguori | else if (speed <= 75 * MARGIN) |
993 | 6f97dba0 | aliguori | spd = B75; |
994 | 6f97dba0 | aliguori | else if (speed <= 300 * MARGIN) |
995 | 6f97dba0 | aliguori | spd = B300; |
996 | 6f97dba0 | aliguori | else if (speed <= 600 * MARGIN) |
997 | 6f97dba0 | aliguori | spd = B600; |
998 | 6f97dba0 | aliguori | else if (speed <= 1200 * MARGIN) |
999 | 6f97dba0 | aliguori | spd = B1200; |
1000 | 6f97dba0 | aliguori | else if (speed <= 2400 * MARGIN) |
1001 | 6f97dba0 | aliguori | spd = B2400; |
1002 | 6f97dba0 | aliguori | else if (speed <= 4800 * MARGIN) |
1003 | 6f97dba0 | aliguori | spd = B4800; |
1004 | 6f97dba0 | aliguori | else if (speed <= 9600 * MARGIN) |
1005 | 6f97dba0 | aliguori | spd = B9600; |
1006 | 6f97dba0 | aliguori | else if (speed <= 19200 * MARGIN) |
1007 | 6f97dba0 | aliguori | spd = B19200; |
1008 | 6f97dba0 | aliguori | else if (speed <= 38400 * MARGIN) |
1009 | 6f97dba0 | aliguori | spd = B38400; |
1010 | 6f97dba0 | aliguori | else if (speed <= 57600 * MARGIN) |
1011 | 6f97dba0 | aliguori | spd = B57600; |
1012 | 6f97dba0 | aliguori | else if (speed <= 115200 * MARGIN) |
1013 | 6f97dba0 | aliguori | spd = B115200; |
1014 | 6f97dba0 | aliguori | else
|
1015 | 6f97dba0 | aliguori | spd = B115200; |
1016 | 6f97dba0 | aliguori | |
1017 | 6f97dba0 | aliguori | cfsetispeed(&tty, spd); |
1018 | 6f97dba0 | aliguori | cfsetospeed(&tty, spd); |
1019 | 6f97dba0 | aliguori | |
1020 | 6f97dba0 | aliguori | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
1021 | 6f97dba0 | aliguori | |INLCR|IGNCR|ICRNL|IXON); |
1022 | 6f97dba0 | aliguori | tty.c_oflag |= OPOST; |
1023 | 6f97dba0 | aliguori | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); |
1024 | 6f97dba0 | aliguori | tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); |
1025 | 6f97dba0 | aliguori | switch(data_bits) {
|
1026 | 6f97dba0 | aliguori | default:
|
1027 | 6f97dba0 | aliguori | case 8: |
1028 | 6f97dba0 | aliguori | tty.c_cflag |= CS8; |
1029 | 6f97dba0 | aliguori | break;
|
1030 | 6f97dba0 | aliguori | case 7: |
1031 | 6f97dba0 | aliguori | tty.c_cflag |= CS7; |
1032 | 6f97dba0 | aliguori | break;
|
1033 | 6f97dba0 | aliguori | case 6: |
1034 | 6f97dba0 | aliguori | tty.c_cflag |= CS6; |
1035 | 6f97dba0 | aliguori | break;
|
1036 | 6f97dba0 | aliguori | case 5: |
1037 | 6f97dba0 | aliguori | tty.c_cflag |= CS5; |
1038 | 6f97dba0 | aliguori | break;
|
1039 | 6f97dba0 | aliguori | } |
1040 | 6f97dba0 | aliguori | switch(parity) {
|
1041 | 6f97dba0 | aliguori | default:
|
1042 | 6f97dba0 | aliguori | case 'N': |
1043 | 6f97dba0 | aliguori | break;
|
1044 | 6f97dba0 | aliguori | case 'E': |
1045 | 6f97dba0 | aliguori | tty.c_cflag |= PARENB; |
1046 | 6f97dba0 | aliguori | break;
|
1047 | 6f97dba0 | aliguori | case 'O': |
1048 | 6f97dba0 | aliguori | tty.c_cflag |= PARENB | PARODD; |
1049 | 6f97dba0 | aliguori | break;
|
1050 | 6f97dba0 | aliguori | } |
1051 | 6f97dba0 | aliguori | if (stop_bits == 2) |
1052 | 6f97dba0 | aliguori | tty.c_cflag |= CSTOPB; |
1053 | 6f97dba0 | aliguori | |
1054 | 6f97dba0 | aliguori | tcsetattr (fd, TCSANOW, &tty); |
1055 | 6f97dba0 | aliguori | } |
1056 | 6f97dba0 | aliguori | |
1057 | 6f97dba0 | aliguori | static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) |
1058 | 6f97dba0 | aliguori | { |
1059 | 6f97dba0 | aliguori | FDCharDriver *s = chr->opaque; |
1060 | 6f97dba0 | aliguori | |
1061 | 6f97dba0 | aliguori | switch(cmd) {
|
1062 | 6f97dba0 | aliguori | case CHR_IOCTL_SERIAL_SET_PARAMS:
|
1063 | 6f97dba0 | aliguori | { |
1064 | 6f97dba0 | aliguori | QEMUSerialSetParams *ssp = arg; |
1065 | 6f97dba0 | aliguori | tty_serial_init(s->fd_in, ssp->speed, ssp->parity, |
1066 | 6f97dba0 | aliguori | ssp->data_bits, ssp->stop_bits); |
1067 | 6f97dba0 | aliguori | } |
1068 | 6f97dba0 | aliguori | break;
|
1069 | 6f97dba0 | aliguori | case CHR_IOCTL_SERIAL_SET_BREAK:
|
1070 | 6f97dba0 | aliguori | { |
1071 | 6f97dba0 | aliguori | int enable = *(int *)arg; |
1072 | 6f97dba0 | aliguori | if (enable)
|
1073 | 6f97dba0 | aliguori | tcsendbreak(s->fd_in, 1);
|
1074 | 6f97dba0 | aliguori | } |
1075 | 6f97dba0 | aliguori | break;
|
1076 | 6f97dba0 | aliguori | case CHR_IOCTL_SERIAL_GET_TIOCM:
|
1077 | 6f97dba0 | aliguori | { |
1078 | 6f97dba0 | aliguori | int sarg = 0; |
1079 | 6f97dba0 | aliguori | int *targ = (int *)arg; |
1080 | 6f97dba0 | aliguori | ioctl(s->fd_in, TIOCMGET, &sarg); |
1081 | 6f97dba0 | aliguori | *targ = 0;
|
1082 | b4abdfa4 | aurel32 | if (sarg & TIOCM_CTS)
|
1083 | 6f97dba0 | aliguori | *targ |= CHR_TIOCM_CTS; |
1084 | b4abdfa4 | aurel32 | if (sarg & TIOCM_CAR)
|
1085 | 6f97dba0 | aliguori | *targ |= CHR_TIOCM_CAR; |
1086 | b4abdfa4 | aurel32 | if (sarg & TIOCM_DSR)
|
1087 | 6f97dba0 | aliguori | *targ |= CHR_TIOCM_DSR; |
1088 | b4abdfa4 | aurel32 | if (sarg & TIOCM_RI)
|
1089 | 6f97dba0 | aliguori | *targ |= CHR_TIOCM_RI; |
1090 | b4abdfa4 | aurel32 | if (sarg & TIOCM_DTR)
|
1091 | 6f97dba0 | aliguori | *targ |= CHR_TIOCM_DTR; |
1092 | b4abdfa4 | aurel32 | if (sarg & TIOCM_RTS)
|
1093 | 6f97dba0 | aliguori | *targ |= CHR_TIOCM_RTS; |
1094 | 6f97dba0 | aliguori | } |
1095 | 6f97dba0 | aliguori | break;
|
1096 | 6f97dba0 | aliguori | case CHR_IOCTL_SERIAL_SET_TIOCM:
|
1097 | 6f97dba0 | aliguori | { |
1098 | 6f97dba0 | aliguori | int sarg = *(int *)arg; |
1099 | 6f97dba0 | aliguori | int targ = 0; |
1100 | b4abdfa4 | aurel32 | ioctl(s->fd_in, TIOCMGET, &targ); |
1101 | b4abdfa4 | aurel32 | targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR |
1102 | b4abdfa4 | aurel32 | | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS); |
1103 | b4abdfa4 | aurel32 | if (sarg & CHR_TIOCM_CTS)
|
1104 | b4abdfa4 | aurel32 | targ |= TIOCM_CTS; |
1105 | b4abdfa4 | aurel32 | if (sarg & CHR_TIOCM_CAR)
|
1106 | b4abdfa4 | aurel32 | targ |= TIOCM_CAR; |
1107 | b4abdfa4 | aurel32 | if (sarg & CHR_TIOCM_DSR)
|
1108 | b4abdfa4 | aurel32 | targ |= TIOCM_DSR; |
1109 | b4abdfa4 | aurel32 | if (sarg & CHR_TIOCM_RI)
|
1110 | b4abdfa4 | aurel32 | targ |= TIOCM_RI; |
1111 | b4abdfa4 | aurel32 | if (sarg & CHR_TIOCM_DTR)
|
1112 | 6f97dba0 | aliguori | targ |= TIOCM_DTR; |
1113 | b4abdfa4 | aurel32 | if (sarg & CHR_TIOCM_RTS)
|
1114 | 6f97dba0 | aliguori | targ |= TIOCM_RTS; |
1115 | 6f97dba0 | aliguori | ioctl(s->fd_in, TIOCMSET, &targ); |
1116 | 6f97dba0 | aliguori | } |
1117 | 6f97dba0 | aliguori | break;
|
1118 | 6f97dba0 | aliguori | default:
|
1119 | 6f97dba0 | aliguori | return -ENOTSUP;
|
1120 | 6f97dba0 | aliguori | } |
1121 | 6f97dba0 | aliguori | return 0; |
1122 | 6f97dba0 | aliguori | } |
1123 | 6f97dba0 | aliguori | |
1124 | 6f97dba0 | aliguori | static CharDriverState *qemu_chr_open_tty(const char *filename) |
1125 | 6f97dba0 | aliguori | { |
1126 | 6f97dba0 | aliguori | CharDriverState *chr; |
1127 | 6f97dba0 | aliguori | int fd;
|
1128 | 6f97dba0 | aliguori | |
1129 | 6f97dba0 | aliguori | TFR(fd = open(filename, O_RDWR | O_NONBLOCK)); |
1130 | 6f97dba0 | aliguori | tty_serial_init(fd, 115200, 'N', 8, 1); |
1131 | 6f97dba0 | aliguori | chr = qemu_chr_open_fd(fd, fd); |
1132 | 6f97dba0 | aliguori | if (!chr) {
|
1133 | 6f97dba0 | aliguori | close(fd); |
1134 | 6f97dba0 | aliguori | return NULL; |
1135 | 6f97dba0 | aliguori | } |
1136 | 6f97dba0 | aliguori | chr->chr_ioctl = tty_serial_ioctl; |
1137 | 6f97dba0 | aliguori | qemu_chr_reset(chr); |
1138 | 6f97dba0 | aliguori | return chr;
|
1139 | 6f97dba0 | aliguori | } |
1140 | 6f97dba0 | aliguori | #else /* ! __linux__ && ! __sun__ */ |
1141 | 6f97dba0 | aliguori | static CharDriverState *qemu_chr_open_pty(void) |
1142 | 6f97dba0 | aliguori | { |
1143 | 6f97dba0 | aliguori | return NULL; |
1144 | 6f97dba0 | aliguori | } |
1145 | 6f97dba0 | aliguori | #endif /* __linux__ || __sun__ */ |
1146 | 6f97dba0 | aliguori | |
1147 | 6f97dba0 | aliguori | #if defined(__linux__)
|
1148 | 6f97dba0 | aliguori | typedef struct { |
1149 | 6f97dba0 | aliguori | int fd;
|
1150 | 6f97dba0 | aliguori | int mode;
|
1151 | 6f97dba0 | aliguori | } ParallelCharDriver; |
1152 | 6f97dba0 | aliguori | |
1153 | 6f97dba0 | aliguori | static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode) |
1154 | 6f97dba0 | aliguori | { |
1155 | 6f97dba0 | aliguori | if (s->mode != mode) {
|
1156 | 6f97dba0 | aliguori | int m = mode;
|
1157 | 6f97dba0 | aliguori | if (ioctl(s->fd, PPSETMODE, &m) < 0) |
1158 | 6f97dba0 | aliguori | return 0; |
1159 | 6f97dba0 | aliguori | s->mode = mode; |
1160 | 6f97dba0 | aliguori | } |
1161 | 6f97dba0 | aliguori | return 1; |
1162 | 6f97dba0 | aliguori | } |
1163 | 6f97dba0 | aliguori | |
1164 | 6f97dba0 | aliguori | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
1165 | 6f97dba0 | aliguori | { |
1166 | 6f97dba0 | aliguori | ParallelCharDriver *drv = chr->opaque; |
1167 | 6f97dba0 | aliguori | int fd = drv->fd;
|
1168 | 6f97dba0 | aliguori | uint8_t b; |
1169 | 6f97dba0 | aliguori | |
1170 | 6f97dba0 | aliguori | switch(cmd) {
|
1171 | 6f97dba0 | aliguori | case CHR_IOCTL_PP_READ_DATA:
|
1172 | 6f97dba0 | aliguori | if (ioctl(fd, PPRDATA, &b) < 0) |
1173 | 6f97dba0 | aliguori | return -ENOTSUP;
|
1174 | 6f97dba0 | aliguori | *(uint8_t *)arg = b; |
1175 | 6f97dba0 | aliguori | break;
|
1176 | 6f97dba0 | aliguori | case CHR_IOCTL_PP_WRITE_DATA:
|
1177 | 6f97dba0 | aliguori | b = *(uint8_t *)arg; |
1178 | 6f97dba0 | aliguori | if (ioctl(fd, PPWDATA, &b) < 0) |
1179 | 6f97dba0 | aliguori | return -ENOTSUP;
|
1180 | 6f97dba0 | aliguori | break;
|
1181 | 6f97dba0 | aliguori | case CHR_IOCTL_PP_READ_CONTROL:
|
1182 | 6f97dba0 | aliguori | if (ioctl(fd, PPRCONTROL, &b) < 0) |
1183 | 6f97dba0 | aliguori | return -ENOTSUP;
|
1184 | 6f97dba0 | aliguori | /* Linux gives only the lowest bits, and no way to know data
|
1185 | 6f97dba0 | aliguori | direction! For better compatibility set the fixed upper
|
1186 | 6f97dba0 | aliguori | bits. */
|
1187 | 6f97dba0 | aliguori | *(uint8_t *)arg = b | 0xc0;
|
1188 | 6f97dba0 | aliguori | break;
|
1189 | 6f97dba0 | aliguori | case CHR_IOCTL_PP_WRITE_CONTROL:
|
1190 | 6f97dba0 | aliguori | b = *(uint8_t *)arg; |
1191 | 6f97dba0 | aliguori | if (ioctl(fd, PPWCONTROL, &b) < 0) |
1192 | 6f97dba0 | aliguori | return -ENOTSUP;
|
1193 | 6f97dba0 | aliguori | break;
|
1194 | 6f97dba0 | aliguori | case CHR_IOCTL_PP_READ_STATUS:
|
1195 | 6f97dba0 | aliguori | if (ioctl(fd, PPRSTATUS, &b) < 0) |
1196 | 6f97dba0 | aliguori | return -ENOTSUP;
|
1197 | 6f97dba0 | aliguori | *(uint8_t *)arg = b; |
1198 | 6f97dba0 | aliguori | break;
|
1199 | 6f97dba0 | aliguori | case CHR_IOCTL_PP_DATA_DIR:
|
1200 | 6f97dba0 | aliguori | if (ioctl(fd, PPDATADIR, (int *)arg) < 0) |
1201 | 6f97dba0 | aliguori | return -ENOTSUP;
|
1202 | 6f97dba0 | aliguori | break;
|
1203 | 6f97dba0 | aliguori | case CHR_IOCTL_PP_EPP_READ_ADDR:
|
1204 | 6f97dba0 | aliguori | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
|
1205 | 6f97dba0 | aliguori | struct ParallelIOArg *parg = arg;
|
1206 | 6f97dba0 | aliguori | int n = read(fd, parg->buffer, parg->count);
|
1207 | 6f97dba0 | aliguori | if (n != parg->count) {
|
1208 | 6f97dba0 | aliguori | return -EIO;
|
1209 | 6f97dba0 | aliguori | } |
1210 | 6f97dba0 | aliguori | } |
1211 | 6f97dba0 | aliguori | break;
|
1212 | 6f97dba0 | aliguori | case CHR_IOCTL_PP_EPP_READ:
|
1213 | 6f97dba0 | aliguori | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
|
1214 | 6f97dba0 | aliguori | struct ParallelIOArg *parg = arg;
|
1215 | 6f97dba0 | aliguori | int n = read(fd, parg->buffer, parg->count);
|
1216 | 6f97dba0 | aliguori | if (n != parg->count) {
|
1217 | 6f97dba0 | aliguori | return -EIO;
|
1218 | 6f97dba0 | aliguori | } |
1219 | 6f97dba0 | aliguori | } |
1220 | 6f97dba0 | aliguori | break;
|
1221 | 6f97dba0 | aliguori | case CHR_IOCTL_PP_EPP_WRITE_ADDR:
|
1222 | 6f97dba0 | aliguori | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
|
1223 | 6f97dba0 | aliguori | struct ParallelIOArg *parg = arg;
|
1224 | 6f97dba0 | aliguori | int n = write(fd, parg->buffer, parg->count);
|
1225 | 6f97dba0 | aliguori | if (n != parg->count) {
|
1226 | 6f97dba0 | aliguori | return -EIO;
|
1227 | 6f97dba0 | aliguori | } |
1228 | 6f97dba0 | aliguori | } |
1229 | 6f97dba0 | aliguori | break;
|
1230 | 6f97dba0 | aliguori | case CHR_IOCTL_PP_EPP_WRITE:
|
1231 | 6f97dba0 | aliguori | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
|
1232 | 6f97dba0 | aliguori | struct ParallelIOArg *parg = arg;
|
1233 | 6f97dba0 | aliguori | int n = write(fd, parg->buffer, parg->count);
|
1234 | 6f97dba0 | aliguori | if (n != parg->count) {
|
1235 | 6f97dba0 | aliguori | return -EIO;
|
1236 | 6f97dba0 | aliguori | } |
1237 | 6f97dba0 | aliguori | } |
1238 | 6f97dba0 | aliguori | break;
|
1239 | 6f97dba0 | aliguori | default:
|
1240 | 6f97dba0 | aliguori | return -ENOTSUP;
|
1241 | 6f97dba0 | aliguori | } |
1242 | 6f97dba0 | aliguori | return 0; |
1243 | 6f97dba0 | aliguori | } |
1244 | 6f97dba0 | aliguori | |
1245 | 6f97dba0 | aliguori | static void pp_close(CharDriverState *chr) |
1246 | 6f97dba0 | aliguori | { |
1247 | 6f97dba0 | aliguori | ParallelCharDriver *drv = chr->opaque; |
1248 | 6f97dba0 | aliguori | int fd = drv->fd;
|
1249 | 6f97dba0 | aliguori | |
1250 | 6f97dba0 | aliguori | pp_hw_mode(drv, IEEE1284_MODE_COMPAT); |
1251 | 6f97dba0 | aliguori | ioctl(fd, PPRELEASE); |
1252 | 6f97dba0 | aliguori | close(fd); |
1253 | 6f97dba0 | aliguori | qemu_free(drv); |
1254 | 6f97dba0 | aliguori | } |
1255 | 6f97dba0 | aliguori | |
1256 | 6f97dba0 | aliguori | static CharDriverState *qemu_chr_open_pp(const char *filename) |
1257 | 6f97dba0 | aliguori | { |
1258 | 6f97dba0 | aliguori | CharDriverState *chr; |
1259 | 6f97dba0 | aliguori | ParallelCharDriver *drv; |
1260 | 6f97dba0 | aliguori | int fd;
|
1261 | 6f97dba0 | aliguori | |
1262 | 6f97dba0 | aliguori | TFR(fd = open(filename, O_RDWR)); |
1263 | 6f97dba0 | aliguori | if (fd < 0) |
1264 | 6f97dba0 | aliguori | return NULL; |
1265 | 6f97dba0 | aliguori | |
1266 | 6f97dba0 | aliguori | if (ioctl(fd, PPCLAIM) < 0) { |
1267 | 6f97dba0 | aliguori | close(fd); |
1268 | 6f97dba0 | aliguori | return NULL; |
1269 | 6f97dba0 | aliguori | } |
1270 | 6f97dba0 | aliguori | |
1271 | 6f97dba0 | aliguori | drv = qemu_mallocz(sizeof(ParallelCharDriver));
|
1272 | 6f97dba0 | aliguori | drv->fd = fd; |
1273 | 6f97dba0 | aliguori | drv->mode = IEEE1284_MODE_COMPAT; |
1274 | 6f97dba0 | aliguori | |
1275 | 6f97dba0 | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
1276 | 6f97dba0 | aliguori | chr->chr_write = null_chr_write; |
1277 | 6f97dba0 | aliguori | chr->chr_ioctl = pp_ioctl; |
1278 | 6f97dba0 | aliguori | chr->chr_close = pp_close; |
1279 | 6f97dba0 | aliguori | chr->opaque = drv; |
1280 | 6f97dba0 | aliguori | |
1281 | 6f97dba0 | aliguori | qemu_chr_reset(chr); |
1282 | 6f97dba0 | aliguori | |
1283 | 6f97dba0 | aliguori | return chr;
|
1284 | 6f97dba0 | aliguori | } |
1285 | 6f97dba0 | aliguori | #endif /* __linux__ */ |
1286 | 6f97dba0 | aliguori | |
1287 | c5e97233 | blueswir1 | #if defined(__FreeBSD__) || defined(__DragonFly__)
|
1288 | 6972f935 | blueswir1 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
1289 | 6972f935 | blueswir1 | { |
1290 | 6972f935 | blueswir1 | int fd = (int)chr->opaque; |
1291 | 6972f935 | blueswir1 | uint8_t b; |
1292 | 6972f935 | blueswir1 | |
1293 | 6972f935 | blueswir1 | switch(cmd) {
|
1294 | 6972f935 | blueswir1 | case CHR_IOCTL_PP_READ_DATA:
|
1295 | 6972f935 | blueswir1 | if (ioctl(fd, PPIGDATA, &b) < 0) |
1296 | 6972f935 | blueswir1 | return -ENOTSUP;
|
1297 | 6972f935 | blueswir1 | *(uint8_t *)arg = b; |
1298 | 6972f935 | blueswir1 | break;
|
1299 | 6972f935 | blueswir1 | case CHR_IOCTL_PP_WRITE_DATA:
|
1300 | 6972f935 | blueswir1 | b = *(uint8_t *)arg; |
1301 | 6972f935 | blueswir1 | if (ioctl(fd, PPISDATA, &b) < 0) |
1302 | 6972f935 | blueswir1 | return -ENOTSUP;
|
1303 | 6972f935 | blueswir1 | break;
|
1304 | 6972f935 | blueswir1 | case CHR_IOCTL_PP_READ_CONTROL:
|
1305 | 6972f935 | blueswir1 | if (ioctl(fd, PPIGCTRL, &b) < 0) |
1306 | 6972f935 | blueswir1 | return -ENOTSUP;
|
1307 | 6972f935 | blueswir1 | *(uint8_t *)arg = b; |
1308 | 6972f935 | blueswir1 | break;
|
1309 | 6972f935 | blueswir1 | case CHR_IOCTL_PP_WRITE_CONTROL:
|
1310 | 6972f935 | blueswir1 | b = *(uint8_t *)arg; |
1311 | 6972f935 | blueswir1 | if (ioctl(fd, PPISCTRL, &b) < 0) |
1312 | 6972f935 | blueswir1 | return -ENOTSUP;
|
1313 | 6972f935 | blueswir1 | break;
|
1314 | 6972f935 | blueswir1 | case CHR_IOCTL_PP_READ_STATUS:
|
1315 | 6972f935 | blueswir1 | if (ioctl(fd, PPIGSTATUS, &b) < 0) |
1316 | 6972f935 | blueswir1 | return -ENOTSUP;
|
1317 | 6972f935 | blueswir1 | *(uint8_t *)arg = b; |
1318 | 6972f935 | blueswir1 | break;
|
1319 | 6972f935 | blueswir1 | default:
|
1320 | 6972f935 | blueswir1 | return -ENOTSUP;
|
1321 | 6972f935 | blueswir1 | } |
1322 | 6972f935 | blueswir1 | return 0; |
1323 | 6972f935 | blueswir1 | } |
1324 | 6972f935 | blueswir1 | |
1325 | 6972f935 | blueswir1 | static CharDriverState *qemu_chr_open_pp(const char *filename) |
1326 | 6972f935 | blueswir1 | { |
1327 | 6972f935 | blueswir1 | CharDriverState *chr; |
1328 | 6972f935 | blueswir1 | int fd;
|
1329 | 6972f935 | blueswir1 | |
1330 | 6972f935 | blueswir1 | fd = open(filename, O_RDWR); |
1331 | 6972f935 | blueswir1 | if (fd < 0) |
1332 | 6972f935 | blueswir1 | return NULL; |
1333 | 6972f935 | blueswir1 | |
1334 | 6972f935 | blueswir1 | chr = qemu_mallocz(sizeof(CharDriverState));
|
1335 | 6972f935 | blueswir1 | chr->opaque = (void *)fd;
|
1336 | 6972f935 | blueswir1 | chr->chr_write = null_chr_write; |
1337 | 6972f935 | blueswir1 | chr->chr_ioctl = pp_ioctl; |
1338 | 6972f935 | blueswir1 | return chr;
|
1339 | 6972f935 | blueswir1 | } |
1340 | 6972f935 | blueswir1 | #endif
|
1341 | 6972f935 | blueswir1 | |
1342 | 6f97dba0 | aliguori | #else /* _WIN32 */ |
1343 | 6f97dba0 | aliguori | |
1344 | 6f97dba0 | aliguori | typedef struct { |
1345 | 6f97dba0 | aliguori | int max_size;
|
1346 | 6f97dba0 | aliguori | HANDLE hcom, hrecv, hsend; |
1347 | 6f97dba0 | aliguori | OVERLAPPED orecv, osend; |
1348 | 6f97dba0 | aliguori | BOOL fpipe; |
1349 | 6f97dba0 | aliguori | DWORD len; |
1350 | 6f97dba0 | aliguori | } WinCharState; |
1351 | 6f97dba0 | aliguori | |
1352 | 6f97dba0 | aliguori | #define NSENDBUF 2048 |
1353 | 6f97dba0 | aliguori | #define NRECVBUF 2048 |
1354 | 6f97dba0 | aliguori | #define MAXCONNECT 1 |
1355 | 6f97dba0 | aliguori | #define NTIMEOUT 5000 |
1356 | 6f97dba0 | aliguori | |
1357 | 6f97dba0 | aliguori | static int win_chr_poll(void *opaque); |
1358 | 6f97dba0 | aliguori | static int win_chr_pipe_poll(void *opaque); |
1359 | 6f97dba0 | aliguori | |
1360 | 6f97dba0 | aliguori | static void win_chr_close(CharDriverState *chr) |
1361 | 6f97dba0 | aliguori | { |
1362 | 6f97dba0 | aliguori | WinCharState *s = chr->opaque; |
1363 | 6f97dba0 | aliguori | |
1364 | 6f97dba0 | aliguori | if (s->hsend) {
|
1365 | 6f97dba0 | aliguori | CloseHandle(s->hsend); |
1366 | 6f97dba0 | aliguori | s->hsend = NULL;
|
1367 | 6f97dba0 | aliguori | } |
1368 | 6f97dba0 | aliguori | if (s->hrecv) {
|
1369 | 6f97dba0 | aliguori | CloseHandle(s->hrecv); |
1370 | 6f97dba0 | aliguori | s->hrecv = NULL;
|
1371 | 6f97dba0 | aliguori | } |
1372 | 6f97dba0 | aliguori | if (s->hcom) {
|
1373 | 6f97dba0 | aliguori | CloseHandle(s->hcom); |
1374 | 6f97dba0 | aliguori | s->hcom = NULL;
|
1375 | 6f97dba0 | aliguori | } |
1376 | 6f97dba0 | aliguori | if (s->fpipe)
|
1377 | 6f97dba0 | aliguori | qemu_del_polling_cb(win_chr_pipe_poll, chr); |
1378 | 6f97dba0 | aliguori | else
|
1379 | 6f97dba0 | aliguori | qemu_del_polling_cb(win_chr_poll, chr); |
1380 | 6f97dba0 | aliguori | } |
1381 | 6f97dba0 | aliguori | |
1382 | 6f97dba0 | aliguori | static int win_chr_init(CharDriverState *chr, const char *filename) |
1383 | 6f97dba0 | aliguori | { |
1384 | 6f97dba0 | aliguori | WinCharState *s = chr->opaque; |
1385 | 6f97dba0 | aliguori | COMMCONFIG comcfg; |
1386 | 6f97dba0 | aliguori | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; |
1387 | 6f97dba0 | aliguori | COMSTAT comstat; |
1388 | 6f97dba0 | aliguori | DWORD size; |
1389 | 6f97dba0 | aliguori | DWORD err; |
1390 | 6f97dba0 | aliguori | |
1391 | 6f97dba0 | aliguori | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
1392 | 6f97dba0 | aliguori | if (!s->hsend) {
|
1393 | 6f97dba0 | aliguori | fprintf(stderr, "Failed CreateEvent\n");
|
1394 | 6f97dba0 | aliguori | goto fail;
|
1395 | 6f97dba0 | aliguori | } |
1396 | 6f97dba0 | aliguori | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
1397 | 6f97dba0 | aliguori | if (!s->hrecv) {
|
1398 | 6f97dba0 | aliguori | fprintf(stderr, "Failed CreateEvent\n");
|
1399 | 6f97dba0 | aliguori | goto fail;
|
1400 | 6f97dba0 | aliguori | } |
1401 | 6f97dba0 | aliguori | |
1402 | 6f97dba0 | aliguori | s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, |
1403 | 6f97dba0 | aliguori | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
|
1404 | 6f97dba0 | aliguori | if (s->hcom == INVALID_HANDLE_VALUE) {
|
1405 | 6f97dba0 | aliguori | fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
|
1406 | 6f97dba0 | aliguori | s->hcom = NULL;
|
1407 | 6f97dba0 | aliguori | goto fail;
|
1408 | 6f97dba0 | aliguori | } |
1409 | 6f97dba0 | aliguori | |
1410 | 6f97dba0 | aliguori | if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
|
1411 | 6f97dba0 | aliguori | fprintf(stderr, "Failed SetupComm\n");
|
1412 | 6f97dba0 | aliguori | goto fail;
|
1413 | 6f97dba0 | aliguori | } |
1414 | 6f97dba0 | aliguori | |
1415 | 6f97dba0 | aliguori | ZeroMemory(&comcfg, sizeof(COMMCONFIG));
|
1416 | 6f97dba0 | aliguori | size = sizeof(COMMCONFIG);
|
1417 | 6f97dba0 | aliguori | GetDefaultCommConfig(filename, &comcfg, &size); |
1418 | 6f97dba0 | aliguori | comcfg.dcb.DCBlength = sizeof(DCB);
|
1419 | 6f97dba0 | aliguori | CommConfigDialog(filename, NULL, &comcfg);
|
1420 | 6f97dba0 | aliguori | |
1421 | 6f97dba0 | aliguori | if (!SetCommState(s->hcom, &comcfg.dcb)) {
|
1422 | 6f97dba0 | aliguori | fprintf(stderr, "Failed SetCommState\n");
|
1423 | 6f97dba0 | aliguori | goto fail;
|
1424 | 6f97dba0 | aliguori | } |
1425 | 6f97dba0 | aliguori | |
1426 | 6f97dba0 | aliguori | if (!SetCommMask(s->hcom, EV_ERR)) {
|
1427 | 6f97dba0 | aliguori | fprintf(stderr, "Failed SetCommMask\n");
|
1428 | 6f97dba0 | aliguori | goto fail;
|
1429 | 6f97dba0 | aliguori | } |
1430 | 6f97dba0 | aliguori | |
1431 | 6f97dba0 | aliguori | cto.ReadIntervalTimeout = MAXDWORD; |
1432 | 6f97dba0 | aliguori | if (!SetCommTimeouts(s->hcom, &cto)) {
|
1433 | 6f97dba0 | aliguori | fprintf(stderr, "Failed SetCommTimeouts\n");
|
1434 | 6f97dba0 | aliguori | goto fail;
|
1435 | 6f97dba0 | aliguori | } |
1436 | 6f97dba0 | aliguori | |
1437 | 6f97dba0 | aliguori | if (!ClearCommError(s->hcom, &err, &comstat)) {
|
1438 | 6f97dba0 | aliguori | fprintf(stderr, "Failed ClearCommError\n");
|
1439 | 6f97dba0 | aliguori | goto fail;
|
1440 | 6f97dba0 | aliguori | } |
1441 | 6f97dba0 | aliguori | qemu_add_polling_cb(win_chr_poll, chr); |
1442 | 6f97dba0 | aliguori | return 0; |
1443 | 6f97dba0 | aliguori | |
1444 | 6f97dba0 | aliguori | fail:
|
1445 | 6f97dba0 | aliguori | win_chr_close(chr); |
1446 | 6f97dba0 | aliguori | return -1; |
1447 | 6f97dba0 | aliguori | } |
1448 | 6f97dba0 | aliguori | |
1449 | 6f97dba0 | aliguori | static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) |
1450 | 6f97dba0 | aliguori | { |
1451 | 6f97dba0 | aliguori | WinCharState *s = chr->opaque; |
1452 | 6f97dba0 | aliguori | DWORD len, ret, size, err; |
1453 | 6f97dba0 | aliguori | |
1454 | 6f97dba0 | aliguori | len = len1; |
1455 | 6f97dba0 | aliguori | ZeroMemory(&s->osend, sizeof(s->osend));
|
1456 | 6f97dba0 | aliguori | s->osend.hEvent = s->hsend; |
1457 | 6f97dba0 | aliguori | while (len > 0) { |
1458 | 6f97dba0 | aliguori | if (s->hsend)
|
1459 | 6f97dba0 | aliguori | ret = WriteFile(s->hcom, buf, len, &size, &s->osend); |
1460 | 6f97dba0 | aliguori | else
|
1461 | 6f97dba0 | aliguori | ret = WriteFile(s->hcom, buf, len, &size, NULL);
|
1462 | 6f97dba0 | aliguori | if (!ret) {
|
1463 | 6f97dba0 | aliguori | err = GetLastError(); |
1464 | 6f97dba0 | aliguori | if (err == ERROR_IO_PENDING) {
|
1465 | 6f97dba0 | aliguori | ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); |
1466 | 6f97dba0 | aliguori | if (ret) {
|
1467 | 6f97dba0 | aliguori | buf += size; |
1468 | 6f97dba0 | aliguori | len -= size; |
1469 | 6f97dba0 | aliguori | } else {
|
1470 | 6f97dba0 | aliguori | break;
|
1471 | 6f97dba0 | aliguori | } |
1472 | 6f97dba0 | aliguori | } else {
|
1473 | 6f97dba0 | aliguori | break;
|
1474 | 6f97dba0 | aliguori | } |
1475 | 6f97dba0 | aliguori | } else {
|
1476 | 6f97dba0 | aliguori | buf += size; |
1477 | 6f97dba0 | aliguori | len -= size; |
1478 | 6f97dba0 | aliguori | } |
1479 | 6f97dba0 | aliguori | } |
1480 | 6f97dba0 | aliguori | return len1 - len;
|
1481 | 6f97dba0 | aliguori | } |
1482 | 6f97dba0 | aliguori | |
1483 | 6f97dba0 | aliguori | static int win_chr_read_poll(CharDriverState *chr) |
1484 | 6f97dba0 | aliguori | { |
1485 | 6f97dba0 | aliguori | WinCharState *s = chr->opaque; |
1486 | 6f97dba0 | aliguori | |
1487 | 6f97dba0 | aliguori | s->max_size = qemu_chr_can_read(chr); |
1488 | 6f97dba0 | aliguori | return s->max_size;
|
1489 | 6f97dba0 | aliguori | } |
1490 | 6f97dba0 | aliguori | |
1491 | 6f97dba0 | aliguori | static void win_chr_readfile(CharDriverState *chr) |
1492 | 6f97dba0 | aliguori | { |
1493 | 6f97dba0 | aliguori | WinCharState *s = chr->opaque; |
1494 | 6f97dba0 | aliguori | int ret, err;
|
1495 | 6f97dba0 | aliguori | uint8_t buf[1024];
|
1496 | 6f97dba0 | aliguori | DWORD size; |
1497 | 6f97dba0 | aliguori | |
1498 | 6f97dba0 | aliguori | ZeroMemory(&s->orecv, sizeof(s->orecv));
|
1499 | 6f97dba0 | aliguori | s->orecv.hEvent = s->hrecv; |
1500 | 6f97dba0 | aliguori | ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); |
1501 | 6f97dba0 | aliguori | if (!ret) {
|
1502 | 6f97dba0 | aliguori | err = GetLastError(); |
1503 | 6f97dba0 | aliguori | if (err == ERROR_IO_PENDING) {
|
1504 | 6f97dba0 | aliguori | ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); |
1505 | 6f97dba0 | aliguori | } |
1506 | 6f97dba0 | aliguori | } |
1507 | 6f97dba0 | aliguori | |
1508 | 6f97dba0 | aliguori | if (size > 0) { |
1509 | 6f97dba0 | aliguori | qemu_chr_read(chr, buf, size); |
1510 | 6f97dba0 | aliguori | } |
1511 | 6f97dba0 | aliguori | } |
1512 | 6f97dba0 | aliguori | |
1513 | 6f97dba0 | aliguori | static void win_chr_read(CharDriverState *chr) |
1514 | 6f97dba0 | aliguori | { |
1515 | 6f97dba0 | aliguori | WinCharState *s = chr->opaque; |
1516 | 6f97dba0 | aliguori | |
1517 | 6f97dba0 | aliguori | if (s->len > s->max_size)
|
1518 | 6f97dba0 | aliguori | s->len = s->max_size; |
1519 | 6f97dba0 | aliguori | if (s->len == 0) |
1520 | 6f97dba0 | aliguori | return;
|
1521 | 6f97dba0 | aliguori | |
1522 | 6f97dba0 | aliguori | win_chr_readfile(chr); |
1523 | 6f97dba0 | aliguori | } |
1524 | 6f97dba0 | aliguori | |
1525 | 6f97dba0 | aliguori | static int win_chr_poll(void *opaque) |
1526 | 6f97dba0 | aliguori | { |
1527 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
1528 | 6f97dba0 | aliguori | WinCharState *s = chr->opaque; |
1529 | 6f97dba0 | aliguori | COMSTAT status; |
1530 | 6f97dba0 | aliguori | DWORD comerr; |
1531 | 6f97dba0 | aliguori | |
1532 | 6f97dba0 | aliguori | ClearCommError(s->hcom, &comerr, &status); |
1533 | 6f97dba0 | aliguori | if (status.cbInQue > 0) { |
1534 | 6f97dba0 | aliguori | s->len = status.cbInQue; |
1535 | 6f97dba0 | aliguori | win_chr_read_poll(chr); |
1536 | 6f97dba0 | aliguori | win_chr_read(chr); |
1537 | 6f97dba0 | aliguori | return 1; |
1538 | 6f97dba0 | aliguori | } |
1539 | 6f97dba0 | aliguori | return 0; |
1540 | 6f97dba0 | aliguori | } |
1541 | 6f97dba0 | aliguori | |
1542 | 6f97dba0 | aliguori | static CharDriverState *qemu_chr_open_win(const char *filename) |
1543 | 6f97dba0 | aliguori | { |
1544 | 6f97dba0 | aliguori | CharDriverState *chr; |
1545 | 6f97dba0 | aliguori | WinCharState *s; |
1546 | 6f97dba0 | aliguori | |
1547 | 6f97dba0 | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
1548 | 6f97dba0 | aliguori | s = qemu_mallocz(sizeof(WinCharState));
|
1549 | 6f97dba0 | aliguori | chr->opaque = s; |
1550 | 6f97dba0 | aliguori | chr->chr_write = win_chr_write; |
1551 | 6f97dba0 | aliguori | chr->chr_close = win_chr_close; |
1552 | 6f97dba0 | aliguori | |
1553 | 6f97dba0 | aliguori | if (win_chr_init(chr, filename) < 0) { |
1554 | 6f97dba0 | aliguori | free(s); |
1555 | 6f97dba0 | aliguori | free(chr); |
1556 | 6f97dba0 | aliguori | return NULL; |
1557 | 6f97dba0 | aliguori | } |
1558 | 6f97dba0 | aliguori | qemu_chr_reset(chr); |
1559 | 6f97dba0 | aliguori | return chr;
|
1560 | 6f97dba0 | aliguori | } |
1561 | 6f97dba0 | aliguori | |
1562 | 6f97dba0 | aliguori | static int win_chr_pipe_poll(void *opaque) |
1563 | 6f97dba0 | aliguori | { |
1564 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
1565 | 6f97dba0 | aliguori | WinCharState *s = chr->opaque; |
1566 | 6f97dba0 | aliguori | DWORD size; |
1567 | 6f97dba0 | aliguori | |
1568 | 6f97dba0 | aliguori | PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); |
1569 | 6f97dba0 | aliguori | if (size > 0) { |
1570 | 6f97dba0 | aliguori | s->len = size; |
1571 | 6f97dba0 | aliguori | win_chr_read_poll(chr); |
1572 | 6f97dba0 | aliguori | win_chr_read(chr); |
1573 | 6f97dba0 | aliguori | return 1; |
1574 | 6f97dba0 | aliguori | } |
1575 | 6f97dba0 | aliguori | return 0; |
1576 | 6f97dba0 | aliguori | } |
1577 | 6f97dba0 | aliguori | |
1578 | 6f97dba0 | aliguori | static int win_chr_pipe_init(CharDriverState *chr, const char *filename) |
1579 | 6f97dba0 | aliguori | { |
1580 | 6f97dba0 | aliguori | WinCharState *s = chr->opaque; |
1581 | 6f97dba0 | aliguori | OVERLAPPED ov; |
1582 | 6f97dba0 | aliguori | int ret;
|
1583 | 6f97dba0 | aliguori | DWORD size; |
1584 | 6f97dba0 | aliguori | char openname[256]; |
1585 | 6f97dba0 | aliguori | |
1586 | 6f97dba0 | aliguori | s->fpipe = TRUE; |
1587 | 6f97dba0 | aliguori | |
1588 | 6f97dba0 | aliguori | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
1589 | 6f97dba0 | aliguori | if (!s->hsend) {
|
1590 | 6f97dba0 | aliguori | fprintf(stderr, "Failed CreateEvent\n");
|
1591 | 6f97dba0 | aliguori | goto fail;
|
1592 | 6f97dba0 | aliguori | } |
1593 | 6f97dba0 | aliguori | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
1594 | 6f97dba0 | aliguori | if (!s->hrecv) {
|
1595 | 6f97dba0 | aliguori | fprintf(stderr, "Failed CreateEvent\n");
|
1596 | 6f97dba0 | aliguori | goto fail;
|
1597 | 6f97dba0 | aliguori | } |
1598 | 6f97dba0 | aliguori | |
1599 | 6f97dba0 | aliguori | snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename); |
1600 | 6f97dba0 | aliguori | s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, |
1601 | 6f97dba0 | aliguori | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | |
1602 | 6f97dba0 | aliguori | PIPE_WAIT, |
1603 | 6f97dba0 | aliguori | MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
|
1604 | 6f97dba0 | aliguori | if (s->hcom == INVALID_HANDLE_VALUE) {
|
1605 | 6f97dba0 | aliguori | fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
|
1606 | 6f97dba0 | aliguori | s->hcom = NULL;
|
1607 | 6f97dba0 | aliguori | goto fail;
|
1608 | 6f97dba0 | aliguori | } |
1609 | 6f97dba0 | aliguori | |
1610 | 6f97dba0 | aliguori | ZeroMemory(&ov, sizeof(ov));
|
1611 | 6f97dba0 | aliguori | ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
1612 | 6f97dba0 | aliguori | ret = ConnectNamedPipe(s->hcom, &ov); |
1613 | 6f97dba0 | aliguori | if (ret) {
|
1614 | 6f97dba0 | aliguori | fprintf(stderr, "Failed ConnectNamedPipe\n");
|
1615 | 6f97dba0 | aliguori | goto fail;
|
1616 | 6f97dba0 | aliguori | } |
1617 | 6f97dba0 | aliguori | |
1618 | 6f97dba0 | aliguori | ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); |
1619 | 6f97dba0 | aliguori | if (!ret) {
|
1620 | 6f97dba0 | aliguori | fprintf(stderr, "Failed GetOverlappedResult\n");
|
1621 | 6f97dba0 | aliguori | if (ov.hEvent) {
|
1622 | 6f97dba0 | aliguori | CloseHandle(ov.hEvent); |
1623 | 6f97dba0 | aliguori | ov.hEvent = NULL;
|
1624 | 6f97dba0 | aliguori | } |
1625 | 6f97dba0 | aliguori | goto fail;
|
1626 | 6f97dba0 | aliguori | } |
1627 | 6f97dba0 | aliguori | |
1628 | 6f97dba0 | aliguori | if (ov.hEvent) {
|
1629 | 6f97dba0 | aliguori | CloseHandle(ov.hEvent); |
1630 | 6f97dba0 | aliguori | ov.hEvent = NULL;
|
1631 | 6f97dba0 | aliguori | } |
1632 | 6f97dba0 | aliguori | qemu_add_polling_cb(win_chr_pipe_poll, chr); |
1633 | 6f97dba0 | aliguori | return 0; |
1634 | 6f97dba0 | aliguori | |
1635 | 6f97dba0 | aliguori | fail:
|
1636 | 6f97dba0 | aliguori | win_chr_close(chr); |
1637 | 6f97dba0 | aliguori | return -1; |
1638 | 6f97dba0 | aliguori | } |
1639 | 6f97dba0 | aliguori | |
1640 | 6f97dba0 | aliguori | |
1641 | 6f97dba0 | aliguori | static CharDriverState *qemu_chr_open_win_pipe(const char *filename) |
1642 | 6f97dba0 | aliguori | { |
1643 | 6f97dba0 | aliguori | CharDriverState *chr; |
1644 | 6f97dba0 | aliguori | WinCharState *s; |
1645 | 6f97dba0 | aliguori | |
1646 | 6f97dba0 | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
1647 | 6f97dba0 | aliguori | s = qemu_mallocz(sizeof(WinCharState));
|
1648 | 6f97dba0 | aliguori | chr->opaque = s; |
1649 | 6f97dba0 | aliguori | chr->chr_write = win_chr_write; |
1650 | 6f97dba0 | aliguori | chr->chr_close = win_chr_close; |
1651 | 6f97dba0 | aliguori | |
1652 | 6f97dba0 | aliguori | if (win_chr_pipe_init(chr, filename) < 0) { |
1653 | 6f97dba0 | aliguori | free(s); |
1654 | 6f97dba0 | aliguori | free(chr); |
1655 | 6f97dba0 | aliguori | return NULL; |
1656 | 6f97dba0 | aliguori | } |
1657 | 6f97dba0 | aliguori | qemu_chr_reset(chr); |
1658 | 6f97dba0 | aliguori | return chr;
|
1659 | 6f97dba0 | aliguori | } |
1660 | 6f97dba0 | aliguori | |
1661 | 6f97dba0 | aliguori | static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
|
1662 | 6f97dba0 | aliguori | { |
1663 | 6f97dba0 | aliguori | CharDriverState *chr; |
1664 | 6f97dba0 | aliguori | WinCharState *s; |
1665 | 6f97dba0 | aliguori | |
1666 | 6f97dba0 | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
1667 | 6f97dba0 | aliguori | s = qemu_mallocz(sizeof(WinCharState));
|
1668 | 6f97dba0 | aliguori | s->hcom = fd_out; |
1669 | 6f97dba0 | aliguori | chr->opaque = s; |
1670 | 6f97dba0 | aliguori | chr->chr_write = win_chr_write; |
1671 | 6f97dba0 | aliguori | qemu_chr_reset(chr); |
1672 | 6f97dba0 | aliguori | return chr;
|
1673 | 6f97dba0 | aliguori | } |
1674 | 6f97dba0 | aliguori | |
1675 | 6f97dba0 | aliguori | static CharDriverState *qemu_chr_open_win_con(const char *filename) |
1676 | 6f97dba0 | aliguori | { |
1677 | 6f97dba0 | aliguori | return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
|
1678 | 6f97dba0 | aliguori | } |
1679 | 6f97dba0 | aliguori | |
1680 | 6f97dba0 | aliguori | static CharDriverState *qemu_chr_open_win_file_out(const char *file_out) |
1681 | 6f97dba0 | aliguori | { |
1682 | 6f97dba0 | aliguori | HANDLE fd_out; |
1683 | 6f97dba0 | aliguori | |
1684 | 6f97dba0 | aliguori | fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
|
1685 | 6f97dba0 | aliguori | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
1686 | 6f97dba0 | aliguori | if (fd_out == INVALID_HANDLE_VALUE)
|
1687 | 6f97dba0 | aliguori | return NULL; |
1688 | 6f97dba0 | aliguori | |
1689 | 6f97dba0 | aliguori | return qemu_chr_open_win_file(fd_out);
|
1690 | 6f97dba0 | aliguori | } |
1691 | 6f97dba0 | aliguori | #endif /* !_WIN32 */ |
1692 | 6f97dba0 | aliguori | |
1693 | 6f97dba0 | aliguori | /***********************************************************/
|
1694 | 6f97dba0 | aliguori | /* UDP Net console */
|
1695 | 6f97dba0 | aliguori | |
1696 | 6f97dba0 | aliguori | typedef struct { |
1697 | 6f97dba0 | aliguori | int fd;
|
1698 | 6f97dba0 | aliguori | struct sockaddr_in daddr;
|
1699 | 6f97dba0 | aliguori | uint8_t buf[1024];
|
1700 | 6f97dba0 | aliguori | int bufcnt;
|
1701 | 6f97dba0 | aliguori | int bufptr;
|
1702 | 6f97dba0 | aliguori | int max_size;
|
1703 | 6f97dba0 | aliguori | } NetCharDriver; |
1704 | 6f97dba0 | aliguori | |
1705 | 6f97dba0 | aliguori | static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1706 | 6f97dba0 | aliguori | { |
1707 | 6f97dba0 | aliguori | NetCharDriver *s = chr->opaque; |
1708 | 6f97dba0 | aliguori | |
1709 | 6f97dba0 | aliguori | return sendto(s->fd, buf, len, 0, |
1710 | 6f97dba0 | aliguori | (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in)); |
1711 | 6f97dba0 | aliguori | } |
1712 | 6f97dba0 | aliguori | |
1713 | 6f97dba0 | aliguori | static int udp_chr_read_poll(void *opaque) |
1714 | 6f97dba0 | aliguori | { |
1715 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
1716 | 6f97dba0 | aliguori | NetCharDriver *s = chr->opaque; |
1717 | 6f97dba0 | aliguori | |
1718 | 6f97dba0 | aliguori | s->max_size = qemu_chr_can_read(chr); |
1719 | 6f97dba0 | aliguori | |
1720 | 6f97dba0 | aliguori | /* If there were any stray characters in the queue process them
|
1721 | 6f97dba0 | aliguori | * first
|
1722 | 6f97dba0 | aliguori | */
|
1723 | 6f97dba0 | aliguori | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
1724 | 6f97dba0 | aliguori | qemu_chr_read(chr, &s->buf[s->bufptr], 1);
|
1725 | 6f97dba0 | aliguori | s->bufptr++; |
1726 | 6f97dba0 | aliguori | s->max_size = qemu_chr_can_read(chr); |
1727 | 6f97dba0 | aliguori | } |
1728 | 6f97dba0 | aliguori | return s->max_size;
|
1729 | 6f97dba0 | aliguori | } |
1730 | 6f97dba0 | aliguori | |
1731 | 6f97dba0 | aliguori | static void udp_chr_read(void *opaque) |
1732 | 6f97dba0 | aliguori | { |
1733 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
1734 | 6f97dba0 | aliguori | NetCharDriver *s = chr->opaque; |
1735 | 6f97dba0 | aliguori | |
1736 | 6f97dba0 | aliguori | if (s->max_size == 0) |
1737 | 6f97dba0 | aliguori | return;
|
1738 | 6f97dba0 | aliguori | s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0); |
1739 | 6f97dba0 | aliguori | s->bufptr = s->bufcnt; |
1740 | 6f97dba0 | aliguori | if (s->bufcnt <= 0) |
1741 | 6f97dba0 | aliguori | return;
|
1742 | 6f97dba0 | aliguori | |
1743 | 6f97dba0 | aliguori | s->bufptr = 0;
|
1744 | 6f97dba0 | aliguori | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
1745 | 6f97dba0 | aliguori | qemu_chr_read(chr, &s->buf[s->bufptr], 1);
|
1746 | 6f97dba0 | aliguori | s->bufptr++; |
1747 | 6f97dba0 | aliguori | s->max_size = qemu_chr_can_read(chr); |
1748 | 6f97dba0 | aliguori | } |
1749 | 6f97dba0 | aliguori | } |
1750 | 6f97dba0 | aliguori | |
1751 | 6f97dba0 | aliguori | static void udp_chr_update_read_handler(CharDriverState *chr) |
1752 | 6f97dba0 | aliguori | { |
1753 | 6f97dba0 | aliguori | NetCharDriver *s = chr->opaque; |
1754 | 6f97dba0 | aliguori | |
1755 | 6f97dba0 | aliguori | if (s->fd >= 0) { |
1756 | 6f97dba0 | aliguori | qemu_set_fd_handler2(s->fd, udp_chr_read_poll, |
1757 | 6f97dba0 | aliguori | udp_chr_read, NULL, chr);
|
1758 | 6f97dba0 | aliguori | } |
1759 | 6f97dba0 | aliguori | } |
1760 | 6f97dba0 | aliguori | |
1761 | 6f97dba0 | aliguori | static CharDriverState *qemu_chr_open_udp(const char *def) |
1762 | 6f97dba0 | aliguori | { |
1763 | 6f97dba0 | aliguori | CharDriverState *chr = NULL;
|
1764 | 6f97dba0 | aliguori | NetCharDriver *s = NULL;
|
1765 | 6f97dba0 | aliguori | int fd = -1; |
1766 | 6f97dba0 | aliguori | struct sockaddr_in saddr;
|
1767 | 6f97dba0 | aliguori | |
1768 | 6f97dba0 | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
1769 | 6f97dba0 | aliguori | s = qemu_mallocz(sizeof(NetCharDriver));
|
1770 | 6f97dba0 | aliguori | |
1771 | 6f97dba0 | aliguori | fd = socket(PF_INET, SOCK_DGRAM, 0);
|
1772 | 6f97dba0 | aliguori | if (fd < 0) { |
1773 | 6f97dba0 | aliguori | perror("socket(PF_INET, SOCK_DGRAM)");
|
1774 | 6f97dba0 | aliguori | goto return_err;
|
1775 | 6f97dba0 | aliguori | } |
1776 | 6f97dba0 | aliguori | |
1777 | 6f97dba0 | aliguori | if (parse_host_src_port(&s->daddr, &saddr, def) < 0) { |
1778 | 6f97dba0 | aliguori | printf("Could not parse: %s\n", def);
|
1779 | 6f97dba0 | aliguori | goto return_err;
|
1780 | 6f97dba0 | aliguori | } |
1781 | 6f97dba0 | aliguori | |
1782 | 6f97dba0 | aliguori | if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) |
1783 | 6f97dba0 | aliguori | { |
1784 | 6f97dba0 | aliguori | perror("bind");
|
1785 | 6f97dba0 | aliguori | goto return_err;
|
1786 | 6f97dba0 | aliguori | } |
1787 | 6f97dba0 | aliguori | |
1788 | 6f97dba0 | aliguori | s->fd = fd; |
1789 | 6f97dba0 | aliguori | s->bufcnt = 0;
|
1790 | 6f97dba0 | aliguori | s->bufptr = 0;
|
1791 | 6f97dba0 | aliguori | chr->opaque = s; |
1792 | 6f97dba0 | aliguori | chr->chr_write = udp_chr_write; |
1793 | 6f97dba0 | aliguori | chr->chr_update_read_handler = udp_chr_update_read_handler; |
1794 | 6f97dba0 | aliguori | return chr;
|
1795 | 6f97dba0 | aliguori | |
1796 | 6f97dba0 | aliguori | return_err:
|
1797 | 6f97dba0 | aliguori | if (chr)
|
1798 | 6f97dba0 | aliguori | free(chr); |
1799 | 6f97dba0 | aliguori | if (s)
|
1800 | 6f97dba0 | aliguori | free(s); |
1801 | 6f97dba0 | aliguori | if (fd >= 0) |
1802 | 6f97dba0 | aliguori | closesocket(fd); |
1803 | 6f97dba0 | aliguori | return NULL; |
1804 | 6f97dba0 | aliguori | } |
1805 | 6f97dba0 | aliguori | |
1806 | 6f97dba0 | aliguori | /***********************************************************/
|
1807 | 6f97dba0 | aliguori | /* TCP Net console */
|
1808 | 6f97dba0 | aliguori | |
1809 | 6f97dba0 | aliguori | typedef struct { |
1810 | 6f97dba0 | aliguori | int fd, listen_fd;
|
1811 | 6f97dba0 | aliguori | int connected;
|
1812 | 6f97dba0 | aliguori | int max_size;
|
1813 | 6f97dba0 | aliguori | int do_telnetopt;
|
1814 | 6f97dba0 | aliguori | int do_nodelay;
|
1815 | 6f97dba0 | aliguori | int is_unix;
|
1816 | 6f97dba0 | aliguori | } TCPCharDriver; |
1817 | 6f97dba0 | aliguori | |
1818 | 6f97dba0 | aliguori | static void tcp_chr_accept(void *opaque); |
1819 | 6f97dba0 | aliguori | |
1820 | 6f97dba0 | aliguori | static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1821 | 6f97dba0 | aliguori | { |
1822 | 6f97dba0 | aliguori | TCPCharDriver *s = chr->opaque; |
1823 | 6f97dba0 | aliguori | if (s->connected) {
|
1824 | 6f97dba0 | aliguori | return send_all(s->fd, buf, len);
|
1825 | 6f97dba0 | aliguori | } else {
|
1826 | 6f97dba0 | aliguori | /* XXX: indicate an error ? */
|
1827 | 6f97dba0 | aliguori | return len;
|
1828 | 6f97dba0 | aliguori | } |
1829 | 6f97dba0 | aliguori | } |
1830 | 6f97dba0 | aliguori | |
1831 | 6f97dba0 | aliguori | static int tcp_chr_read_poll(void *opaque) |
1832 | 6f97dba0 | aliguori | { |
1833 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
1834 | 6f97dba0 | aliguori | TCPCharDriver *s = chr->opaque; |
1835 | 6f97dba0 | aliguori | if (!s->connected)
|
1836 | 6f97dba0 | aliguori | return 0; |
1837 | 6f97dba0 | aliguori | s->max_size = qemu_chr_can_read(chr); |
1838 | 6f97dba0 | aliguori | return s->max_size;
|
1839 | 6f97dba0 | aliguori | } |
1840 | 6f97dba0 | aliguori | |
1841 | 6f97dba0 | aliguori | #define IAC 255 |
1842 | 6f97dba0 | aliguori | #define IAC_BREAK 243 |
1843 | 6f97dba0 | aliguori | static void tcp_chr_process_IAC_bytes(CharDriverState *chr, |
1844 | 6f97dba0 | aliguori | TCPCharDriver *s, |
1845 | 6f97dba0 | aliguori | uint8_t *buf, int *size)
|
1846 | 6f97dba0 | aliguori | { |
1847 | 6f97dba0 | aliguori | /* Handle any telnet client's basic IAC options to satisfy char by
|
1848 | 6f97dba0 | aliguori | * char mode with no echo. All IAC options will be removed from
|
1849 | 6f97dba0 | aliguori | * the buf and the do_telnetopt variable will be used to track the
|
1850 | 6f97dba0 | aliguori | * state of the width of the IAC information.
|
1851 | 6f97dba0 | aliguori | *
|
1852 | 6f97dba0 | aliguori | * IAC commands come in sets of 3 bytes with the exception of the
|
1853 | 6f97dba0 | aliguori | * "IAC BREAK" command and the double IAC.
|
1854 | 6f97dba0 | aliguori | */
|
1855 | 6f97dba0 | aliguori | |
1856 | 6f97dba0 | aliguori | int i;
|
1857 | 6f97dba0 | aliguori | int j = 0; |
1858 | 6f97dba0 | aliguori | |
1859 | 6f97dba0 | aliguori | for (i = 0; i < *size; i++) { |
1860 | 6f97dba0 | aliguori | if (s->do_telnetopt > 1) { |
1861 | 6f97dba0 | aliguori | if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { |
1862 | 6f97dba0 | aliguori | /* Double IAC means send an IAC */
|
1863 | 6f97dba0 | aliguori | if (j != i)
|
1864 | 6f97dba0 | aliguori | buf[j] = buf[i]; |
1865 | 6f97dba0 | aliguori | j++; |
1866 | 6f97dba0 | aliguori | s->do_telnetopt = 1;
|
1867 | 6f97dba0 | aliguori | } else {
|
1868 | 6f97dba0 | aliguori | if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { |
1869 | 6f97dba0 | aliguori | /* Handle IAC break commands by sending a serial break */
|
1870 | 6f97dba0 | aliguori | qemu_chr_event(chr, CHR_EVENT_BREAK); |
1871 | 6f97dba0 | aliguori | s->do_telnetopt++; |
1872 | 6f97dba0 | aliguori | } |
1873 | 6f97dba0 | aliguori | s->do_telnetopt++; |
1874 | 6f97dba0 | aliguori | } |
1875 | 6f97dba0 | aliguori | if (s->do_telnetopt >= 4) { |
1876 | 6f97dba0 | aliguori | s->do_telnetopt = 1;
|
1877 | 6f97dba0 | aliguori | } |
1878 | 6f97dba0 | aliguori | } else {
|
1879 | 6f97dba0 | aliguori | if ((unsigned char)buf[i] == IAC) { |
1880 | 6f97dba0 | aliguori | s->do_telnetopt = 2;
|
1881 | 6f97dba0 | aliguori | } else {
|
1882 | 6f97dba0 | aliguori | if (j != i)
|
1883 | 6f97dba0 | aliguori | buf[j] = buf[i]; |
1884 | 6f97dba0 | aliguori | j++; |
1885 | 6f97dba0 | aliguori | } |
1886 | 6f97dba0 | aliguori | } |
1887 | 6f97dba0 | aliguori | } |
1888 | 6f97dba0 | aliguori | *size = j; |
1889 | 6f97dba0 | aliguori | } |
1890 | 6f97dba0 | aliguori | |
1891 | 6f97dba0 | aliguori | static void tcp_chr_read(void *opaque) |
1892 | 6f97dba0 | aliguori | { |
1893 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
1894 | 6f97dba0 | aliguori | TCPCharDriver *s = chr->opaque; |
1895 | 6f97dba0 | aliguori | uint8_t buf[1024];
|
1896 | 6f97dba0 | aliguori | int len, size;
|
1897 | 6f97dba0 | aliguori | |
1898 | 6f97dba0 | aliguori | if (!s->connected || s->max_size <= 0) |
1899 | 6f97dba0 | aliguori | return;
|
1900 | 6f97dba0 | aliguori | len = sizeof(buf);
|
1901 | 6f97dba0 | aliguori | if (len > s->max_size)
|
1902 | 6f97dba0 | aliguori | len = s->max_size; |
1903 | 6f97dba0 | aliguori | size = recv(s->fd, buf, len, 0);
|
1904 | 6f97dba0 | aliguori | if (size == 0) { |
1905 | 6f97dba0 | aliguori | /* connection closed */
|
1906 | 6f97dba0 | aliguori | s->connected = 0;
|
1907 | 6f97dba0 | aliguori | if (s->listen_fd >= 0) { |
1908 | 6f97dba0 | aliguori | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
|
1909 | 6f97dba0 | aliguori | } |
1910 | 6f97dba0 | aliguori | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
1911 | 6f97dba0 | aliguori | closesocket(s->fd); |
1912 | 6f97dba0 | aliguori | s->fd = -1;
|
1913 | 6f97dba0 | aliguori | } else if (size > 0) { |
1914 | 6f97dba0 | aliguori | if (s->do_telnetopt)
|
1915 | 6f97dba0 | aliguori | tcp_chr_process_IAC_bytes(chr, s, buf, &size); |
1916 | 6f97dba0 | aliguori | if (size > 0) |
1917 | 6f97dba0 | aliguori | qemu_chr_read(chr, buf, size); |
1918 | 6f97dba0 | aliguori | } |
1919 | 6f97dba0 | aliguori | } |
1920 | 6f97dba0 | aliguori | |
1921 | 6f97dba0 | aliguori | static void tcp_chr_connect(void *opaque) |
1922 | 6f97dba0 | aliguori | { |
1923 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
1924 | 6f97dba0 | aliguori | TCPCharDriver *s = chr->opaque; |
1925 | 6f97dba0 | aliguori | |
1926 | 6f97dba0 | aliguori | s->connected = 1;
|
1927 | 6f97dba0 | aliguori | qemu_set_fd_handler2(s->fd, tcp_chr_read_poll, |
1928 | 6f97dba0 | aliguori | tcp_chr_read, NULL, chr);
|
1929 | 6f97dba0 | aliguori | qemu_chr_reset(chr); |
1930 | 6f97dba0 | aliguori | } |
1931 | 6f97dba0 | aliguori | |
1932 | 6f97dba0 | aliguori | #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; |
1933 | 6f97dba0 | aliguori | static void tcp_chr_telnet_init(int fd) |
1934 | 6f97dba0 | aliguori | { |
1935 | 6f97dba0 | aliguori | char buf[3]; |
1936 | 6f97dba0 | aliguori | /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
|
1937 | 6f97dba0 | aliguori | IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ |
1938 | 6f97dba0 | aliguori | send(fd, (char *)buf, 3, 0); |
1939 | 6f97dba0 | aliguori | IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ |
1940 | 6f97dba0 | aliguori | send(fd, (char *)buf, 3, 0); |
1941 | 6f97dba0 | aliguori | IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ |
1942 | 6f97dba0 | aliguori | send(fd, (char *)buf, 3, 0); |
1943 | 6f97dba0 | aliguori | IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ |
1944 | 6f97dba0 | aliguori | send(fd, (char *)buf, 3, 0); |
1945 | 6f97dba0 | aliguori | } |
1946 | 6f97dba0 | aliguori | |
1947 | 6f97dba0 | aliguori | static void socket_set_nodelay(int fd) |
1948 | 6f97dba0 | aliguori | { |
1949 | 6f97dba0 | aliguori | int val = 1; |
1950 | 6f97dba0 | aliguori | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
1951 | 6f97dba0 | aliguori | } |
1952 | 6f97dba0 | aliguori | |
1953 | 6f97dba0 | aliguori | static void tcp_chr_accept(void *opaque) |
1954 | 6f97dba0 | aliguori | { |
1955 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
1956 | 6f97dba0 | aliguori | TCPCharDriver *s = chr->opaque; |
1957 | 6f97dba0 | aliguori | struct sockaddr_in saddr;
|
1958 | 6f97dba0 | aliguori | #ifndef _WIN32
|
1959 | 6f97dba0 | aliguori | struct sockaddr_un uaddr;
|
1960 | 6f97dba0 | aliguori | #endif
|
1961 | 6f97dba0 | aliguori | struct sockaddr *addr;
|
1962 | 6f97dba0 | aliguori | socklen_t len; |
1963 | 6f97dba0 | aliguori | int fd;
|
1964 | 6f97dba0 | aliguori | |
1965 | 6f97dba0 | aliguori | for(;;) {
|
1966 | 6f97dba0 | aliguori | #ifndef _WIN32
|
1967 | 6f97dba0 | aliguori | if (s->is_unix) {
|
1968 | 6f97dba0 | aliguori | len = sizeof(uaddr);
|
1969 | 6f97dba0 | aliguori | addr = (struct sockaddr *)&uaddr;
|
1970 | 6f97dba0 | aliguori | } else
|
1971 | 6f97dba0 | aliguori | #endif
|
1972 | 6f97dba0 | aliguori | { |
1973 | 6f97dba0 | aliguori | len = sizeof(saddr);
|
1974 | 6f97dba0 | aliguori | addr = (struct sockaddr *)&saddr;
|
1975 | 6f97dba0 | aliguori | } |
1976 | 6f97dba0 | aliguori | fd = accept(s->listen_fd, addr, &len); |
1977 | 6f97dba0 | aliguori | if (fd < 0 && errno != EINTR) { |
1978 | 6f97dba0 | aliguori | return;
|
1979 | 6f97dba0 | aliguori | } else if (fd >= 0) { |
1980 | 6f97dba0 | aliguori | if (s->do_telnetopt)
|
1981 | 6f97dba0 | aliguori | tcp_chr_telnet_init(fd); |
1982 | 6f97dba0 | aliguori | break;
|
1983 | 6f97dba0 | aliguori | } |
1984 | 6f97dba0 | aliguori | } |
1985 | 6f97dba0 | aliguori | socket_set_nonblock(fd); |
1986 | 6f97dba0 | aliguori | if (s->do_nodelay)
|
1987 | 6f97dba0 | aliguori | socket_set_nodelay(fd); |
1988 | 6f97dba0 | aliguori | s->fd = fd; |
1989 | 6f97dba0 | aliguori | qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); |
1990 | 6f97dba0 | aliguori | tcp_chr_connect(chr); |
1991 | 6f97dba0 | aliguori | } |
1992 | 6f97dba0 | aliguori | |
1993 | 6f97dba0 | aliguori | static void tcp_chr_close(CharDriverState *chr) |
1994 | 6f97dba0 | aliguori | { |
1995 | 6f97dba0 | aliguori | TCPCharDriver *s = chr->opaque; |
1996 | 6f97dba0 | aliguori | if (s->fd >= 0) |
1997 | 6f97dba0 | aliguori | closesocket(s->fd); |
1998 | 6f97dba0 | aliguori | if (s->listen_fd >= 0) |
1999 | 6f97dba0 | aliguori | closesocket(s->listen_fd); |
2000 | 6f97dba0 | aliguori | qemu_free(s); |
2001 | 6f97dba0 | aliguori | } |
2002 | 6f97dba0 | aliguori | |
2003 | 6f97dba0 | aliguori | static CharDriverState *qemu_chr_open_tcp(const char *host_str, |
2004 | 6f97dba0 | aliguori | int is_telnet,
|
2005 | 6f97dba0 | aliguori | int is_unix)
|
2006 | 6f97dba0 | aliguori | { |
2007 | 6f97dba0 | aliguori | CharDriverState *chr = NULL;
|
2008 | 6f97dba0 | aliguori | TCPCharDriver *s = NULL;
|
2009 | f07b6003 | aliguori | int fd = -1, offset = 0; |
2010 | 6f97dba0 | aliguori | int is_listen = 0; |
2011 | 6f97dba0 | aliguori | int is_waitconnect = 1; |
2012 | 6f97dba0 | aliguori | int do_nodelay = 0; |
2013 | 6f97dba0 | aliguori | const char *ptr; |
2014 | 6f97dba0 | aliguori | |
2015 | 6f97dba0 | aliguori | ptr = host_str; |
2016 | 6f97dba0 | aliguori | while((ptr = strchr(ptr,','))) { |
2017 | 6f97dba0 | aliguori | ptr++; |
2018 | 6f97dba0 | aliguori | if (!strncmp(ptr,"server",6)) { |
2019 | 6f97dba0 | aliguori | is_listen = 1;
|
2020 | 6f97dba0 | aliguori | } else if (!strncmp(ptr,"nowait",6)) { |
2021 | 6f97dba0 | aliguori | is_waitconnect = 0;
|
2022 | 6f97dba0 | aliguori | } else if (!strncmp(ptr,"nodelay",6)) { |
2023 | 6f97dba0 | aliguori | do_nodelay = 1;
|
2024 | f07b6003 | aliguori | } else if (!strncmp(ptr,"to=",3)) { |
2025 | f07b6003 | aliguori | /* nothing, inet_listen() parses this one */;
|
2026 | 84694dd2 | aurel32 | } else if (!strncmp(ptr,"ipv4",4)) { |
2027 | 84694dd2 | aurel32 | /* nothing, inet_connect() and inet_listen() parse this one */;
|
2028 | 84694dd2 | aurel32 | } else if (!strncmp(ptr,"ipv6",4)) { |
2029 | 84694dd2 | aurel32 | /* nothing, inet_connect() and inet_listen() parse this one */;
|
2030 | 6f97dba0 | aliguori | } else {
|
2031 | 6f97dba0 | aliguori | printf("Unknown option: %s\n", ptr);
|
2032 | 6f97dba0 | aliguori | goto fail;
|
2033 | 6f97dba0 | aliguori | } |
2034 | 6f97dba0 | aliguori | } |
2035 | 6f97dba0 | aliguori | if (!is_listen)
|
2036 | 6f97dba0 | aliguori | is_waitconnect = 0;
|
2037 | 6f97dba0 | aliguori | |
2038 | 6f97dba0 | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
2039 | 6f97dba0 | aliguori | s = qemu_mallocz(sizeof(TCPCharDriver));
|
2040 | 6f97dba0 | aliguori | |
2041 | f07b6003 | aliguori | if (is_listen) {
|
2042 | f07b6003 | aliguori | chr->filename = qemu_malloc(256);
|
2043 | f07b6003 | aliguori | if (is_unix) {
|
2044 | 00766a4e | blueswir1 | pstrcpy(chr->filename, 256, "unix:"); |
2045 | f07b6003 | aliguori | } else if (is_telnet) { |
2046 | 00766a4e | blueswir1 | pstrcpy(chr->filename, 256, "telnet:"); |
2047 | f07b6003 | aliguori | } else {
|
2048 | 00766a4e | blueswir1 | pstrcpy(chr->filename, 256, "tcp:"); |
2049 | f07b6003 | aliguori | } |
2050 | f07b6003 | aliguori | offset = strlen(chr->filename); |
2051 | f07b6003 | aliguori | } |
2052 | f07b6003 | aliguori | if (is_unix) {
|
2053 | f07b6003 | aliguori | if (is_listen) {
|
2054 | f07b6003 | aliguori | fd = unix_listen(host_str, chr->filename + offset, 256 - offset);
|
2055 | f07b6003 | aliguori | } else {
|
2056 | f07b6003 | aliguori | fd = unix_connect(host_str); |
2057 | f07b6003 | aliguori | } |
2058 | f07b6003 | aliguori | } else {
|
2059 | f07b6003 | aliguori | if (is_listen) {
|
2060 | f07b6003 | aliguori | fd = inet_listen(host_str, chr->filename + offset, 256 - offset,
|
2061 | f07b6003 | aliguori | SOCK_STREAM, 0);
|
2062 | f07b6003 | aliguori | } else {
|
2063 | f07b6003 | aliguori | fd = inet_connect(host_str, SOCK_STREAM); |
2064 | f07b6003 | aliguori | } |
2065 | f07b6003 | aliguori | } |
2066 | 6f97dba0 | aliguori | if (fd < 0) |
2067 | 6f97dba0 | aliguori | goto fail;
|
2068 | 6f97dba0 | aliguori | |
2069 | 6f97dba0 | aliguori | if (!is_waitconnect)
|
2070 | 6f97dba0 | aliguori | socket_set_nonblock(fd); |
2071 | 6f97dba0 | aliguori | |
2072 | 6f97dba0 | aliguori | s->connected = 0;
|
2073 | 6f97dba0 | aliguori | s->fd = -1;
|
2074 | 6f97dba0 | aliguori | s->listen_fd = -1;
|
2075 | 6f97dba0 | aliguori | s->is_unix = is_unix; |
2076 | 6f97dba0 | aliguori | s->do_nodelay = do_nodelay && !is_unix; |
2077 | 6f97dba0 | aliguori | |
2078 | 6f97dba0 | aliguori | chr->opaque = s; |
2079 | 6f97dba0 | aliguori | chr->chr_write = tcp_chr_write; |
2080 | 6f97dba0 | aliguori | chr->chr_close = tcp_chr_close; |
2081 | 6f97dba0 | aliguori | |
2082 | 6f97dba0 | aliguori | if (is_listen) {
|
2083 | 6f97dba0 | aliguori | s->listen_fd = fd; |
2084 | 6f97dba0 | aliguori | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
|
2085 | 6f97dba0 | aliguori | if (is_telnet)
|
2086 | 6f97dba0 | aliguori | s->do_telnetopt = 1;
|
2087 | 6f97dba0 | aliguori | } else {
|
2088 | f07b6003 | aliguori | s->connected = 1;
|
2089 | 6f97dba0 | aliguori | s->fd = fd; |
2090 | 6f97dba0 | aliguori | socket_set_nodelay(fd); |
2091 | f07b6003 | aliguori | tcp_chr_connect(chr); |
2092 | 6f97dba0 | aliguori | } |
2093 | 6f97dba0 | aliguori | |
2094 | 6f97dba0 | aliguori | if (is_listen && is_waitconnect) {
|
2095 | f07b6003 | aliguori | printf("QEMU waiting for connection on: %s\n",
|
2096 | f07b6003 | aliguori | chr->filename ? chr->filename : host_str); |
2097 | 6f97dba0 | aliguori | tcp_chr_accept(chr); |
2098 | 6f97dba0 | aliguori | socket_set_nonblock(s->listen_fd); |
2099 | 6f97dba0 | aliguori | } |
2100 | 6f97dba0 | aliguori | |
2101 | 6f97dba0 | aliguori | return chr;
|
2102 | 6f97dba0 | aliguori | fail:
|
2103 | 6f97dba0 | aliguori | if (fd >= 0) |
2104 | 6f97dba0 | aliguori | closesocket(fd); |
2105 | 6f97dba0 | aliguori | qemu_free(s); |
2106 | 6f97dba0 | aliguori | qemu_free(chr); |
2107 | 6f97dba0 | aliguori | return NULL; |
2108 | 6f97dba0 | aliguori | } |
2109 | 6f97dba0 | aliguori | |
2110 | ceecf1d1 | aurel32 | CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s)) |
2111 | 6f97dba0 | aliguori | { |
2112 | 6f97dba0 | aliguori | const char *p; |
2113 | 6f97dba0 | aliguori | CharDriverState *chr; |
2114 | 6f97dba0 | aliguori | |
2115 | 6f97dba0 | aliguori | if (!strcmp(filename, "vc")) { |
2116 | 2796dae0 | aliguori | chr = text_console_init(0);
|
2117 | 6f97dba0 | aliguori | } else
|
2118 | 6f97dba0 | aliguori | if (strstart(filename, "vc:", &p)) { |
2119 | 2796dae0 | aliguori | chr = text_console_init(p); |
2120 | 6f97dba0 | aliguori | } else
|
2121 | 6f97dba0 | aliguori | if (!strcmp(filename, "null")) { |
2122 | 6f97dba0 | aliguori | chr = qemu_chr_open_null(); |
2123 | 6f97dba0 | aliguori | } else
|
2124 | 6f97dba0 | aliguori | if (strstart(filename, "tcp:", &p)) { |
2125 | 6f97dba0 | aliguori | chr = qemu_chr_open_tcp(p, 0, 0); |
2126 | 6f97dba0 | aliguori | } else
|
2127 | 6f97dba0 | aliguori | if (strstart(filename, "telnet:", &p)) { |
2128 | 6f97dba0 | aliguori | chr = qemu_chr_open_tcp(p, 1, 0); |
2129 | 6f97dba0 | aliguori | } else
|
2130 | 6f97dba0 | aliguori | if (strstart(filename, "udp:", &p)) { |
2131 | 6f97dba0 | aliguori | chr = qemu_chr_open_udp(p); |
2132 | 6f97dba0 | aliguori | } else
|
2133 | 6f97dba0 | aliguori | if (strstart(filename, "mon:", &p)) { |
2134 | ceecf1d1 | aurel32 | chr = qemu_chr_open(label, p, NULL);
|
2135 | 6f97dba0 | aliguori | if (chr) {
|
2136 | 6f97dba0 | aliguori | chr = qemu_chr_open_mux(chr); |
2137 | cde76ee1 | aliguori | monitor_init(chr, MONITOR_USE_READLINE); |
2138 | 6f97dba0 | aliguori | } else {
|
2139 | 6f97dba0 | aliguori | printf("Unable to open driver: %s\n", p);
|
2140 | 6f97dba0 | aliguori | } |
2141 | aa71cf80 | aurel32 | } else if (!strcmp(filename, "msmouse")) { |
2142 | aa71cf80 | aurel32 | chr = qemu_chr_open_msmouse(); |
2143 | 6f97dba0 | aliguori | } else
|
2144 | 6f97dba0 | aliguori | #ifndef _WIN32
|
2145 | 6f97dba0 | aliguori | if (strstart(filename, "unix:", &p)) { |
2146 | 6f97dba0 | aliguori | chr = qemu_chr_open_tcp(p, 0, 1); |
2147 | 6f97dba0 | aliguori | } else if (strstart(filename, "file:", &p)) { |
2148 | 6f97dba0 | aliguori | chr = qemu_chr_open_file_out(p); |
2149 | 6f97dba0 | aliguori | } else if (strstart(filename, "pipe:", &p)) { |
2150 | 6f97dba0 | aliguori | chr = qemu_chr_open_pipe(p); |
2151 | 6f97dba0 | aliguori | } else if (!strcmp(filename, "pty")) { |
2152 | 6f97dba0 | aliguori | chr = qemu_chr_open_pty(); |
2153 | 6f97dba0 | aliguori | } else if (!strcmp(filename, "stdio")) { |
2154 | 6f97dba0 | aliguori | chr = qemu_chr_open_stdio(); |
2155 | 6f97dba0 | aliguori | } else
|
2156 | 6f97dba0 | aliguori | #if defined(__linux__)
|
2157 | 6f97dba0 | aliguori | if (strstart(filename, "/dev/parport", NULL)) { |
2158 | 6f97dba0 | aliguori | chr = qemu_chr_open_pp(filename); |
2159 | 6f97dba0 | aliguori | } else
|
2160 | c5e97233 | blueswir1 | #elif defined(__FreeBSD__) || defined(__DragonFly__)
|
2161 | 6972f935 | blueswir1 | if (strstart(filename, "/dev/ppi", NULL)) { |
2162 | 6972f935 | blueswir1 | chr = qemu_chr_open_pp(filename); |
2163 | 6972f935 | blueswir1 | } else
|
2164 | 6f97dba0 | aliguori | #endif
|
2165 | 6f97dba0 | aliguori | #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
|
2166 | c5e97233 | blueswir1 | || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) |
2167 | 6f97dba0 | aliguori | if (strstart(filename, "/dev/", NULL)) { |
2168 | 6f97dba0 | aliguori | chr = qemu_chr_open_tty(filename); |
2169 | 6f97dba0 | aliguori | } else
|
2170 | 6f97dba0 | aliguori | #endif
|
2171 | 6f97dba0 | aliguori | #else /* !_WIN32 */ |
2172 | 6f97dba0 | aliguori | if (strstart(filename, "COM", NULL)) { |
2173 | 6f97dba0 | aliguori | chr = qemu_chr_open_win(filename); |
2174 | 6f97dba0 | aliguori | } else
|
2175 | 6f97dba0 | aliguori | if (strstart(filename, "pipe:", &p)) { |
2176 | 6f97dba0 | aliguori | chr = qemu_chr_open_win_pipe(p); |
2177 | 6f97dba0 | aliguori | } else
|
2178 | 6f97dba0 | aliguori | if (strstart(filename, "con:", NULL)) { |
2179 | 6f97dba0 | aliguori | chr = qemu_chr_open_win_con(filename); |
2180 | 6f97dba0 | aliguori | } else
|
2181 | 6f97dba0 | aliguori | if (strstart(filename, "file:", &p)) { |
2182 | 6f97dba0 | aliguori | chr = qemu_chr_open_win_file_out(p); |
2183 | 6f97dba0 | aliguori | } else
|
2184 | 6f97dba0 | aliguori | #endif
|
2185 | 6f97dba0 | aliguori | #ifdef CONFIG_BRLAPI
|
2186 | 6f97dba0 | aliguori | if (!strcmp(filename, "braille")) { |
2187 | 6f97dba0 | aliguori | chr = chr_baum_init(); |
2188 | 6f97dba0 | aliguori | } else
|
2189 | 6f97dba0 | aliguori | #endif
|
2190 | 6f97dba0 | aliguori | { |
2191 | 6f97dba0 | aliguori | chr = NULL;
|
2192 | 6f97dba0 | aliguori | } |
2193 | 6f97dba0 | aliguori | |
2194 | 6f97dba0 | aliguori | if (chr) {
|
2195 | 6f97dba0 | aliguori | if (!chr->filename)
|
2196 | 6f97dba0 | aliguori | chr->filename = qemu_strdup(filename); |
2197 | ceecf1d1 | aurel32 | chr->init = init; |
2198 | 6f97dba0 | aliguori | chr->label = qemu_strdup(label); |
2199 | 6f97dba0 | aliguori | TAILQ_INSERT_TAIL(&chardevs, chr, next); |
2200 | 6f97dba0 | aliguori | } |
2201 | 6f97dba0 | aliguori | return chr;
|
2202 | 6f97dba0 | aliguori | } |
2203 | 6f97dba0 | aliguori | |
2204 | 6f97dba0 | aliguori | void qemu_chr_close(CharDriverState *chr)
|
2205 | 6f97dba0 | aliguori | { |
2206 | 6f97dba0 | aliguori | TAILQ_REMOVE(&chardevs, chr, next); |
2207 | 6f97dba0 | aliguori | if (chr->chr_close)
|
2208 | 6f97dba0 | aliguori | chr->chr_close(chr); |
2209 | 6f97dba0 | aliguori | qemu_free(chr->filename); |
2210 | 6f97dba0 | aliguori | qemu_free(chr->label); |
2211 | 6f97dba0 | aliguori | qemu_free(chr); |
2212 | 6f97dba0 | aliguori | } |
2213 | 6f97dba0 | aliguori | |
2214 | 376253ec | aliguori | void qemu_chr_info(Monitor *mon)
|
2215 | 6f97dba0 | aliguori | { |
2216 | 6f97dba0 | aliguori | CharDriverState *chr; |
2217 | 6f97dba0 | aliguori | |
2218 | 6f97dba0 | aliguori | TAILQ_FOREACH(chr, &chardevs, next) { |
2219 | 376253ec | aliguori | monitor_printf(mon, "%s: filename=%s\n", chr->label, chr->filename);
|
2220 | 6f97dba0 | aliguori | } |
2221 | 6f97dba0 | aliguori | } |