root / qemu-char.c @ dd4239d6
History | View | Annotate | Download (63.6 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 | #include <arpa/inet.h> |
55 | 6f97dba0 | aliguori | #include <dirent.h> |
56 | 6f97dba0 | aliguori | #include <netdb.h> |
57 | 6f97dba0 | aliguori | #include <sys/select.h> |
58 | 71e72a19 | Juan Quintela | #ifdef CONFIG_BSD
|
59 | 6f97dba0 | aliguori | #include <sys/stat.h> |
60 | 24646c7e | blueswir1 | #ifdef __FreeBSD__
|
61 | 6f97dba0 | aliguori | #include <libutil.h> |
62 | 6972f935 | blueswir1 | #include <dev/ppbus/ppi.h> |
63 | 6972f935 | blueswir1 | #include <dev/ppbus/ppbconf.h> |
64 | c5e97233 | blueswir1 | #elif defined(__DragonFly__)
|
65 | c5e97233 | blueswir1 | #include <libutil.h> |
66 | c5e97233 | blueswir1 | #include <dev/misc/ppi/ppi.h> |
67 | c5e97233 | blueswir1 | #include <bus/ppbus/ppbconf.h> |
68 | 24646c7e | blueswir1 | #else
|
69 | 24646c7e | blueswir1 | #include <util.h> |
70 | 6f97dba0 | aliguori | #endif
|
71 | 6f97dba0 | aliguori | #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
|
72 | 6f97dba0 | aliguori | #include <freebsd/stdlib.h> |
73 | 6f97dba0 | aliguori | #else
|
74 | 6f97dba0 | aliguori | #ifdef __linux__
|
75 | 6f97dba0 | aliguori | #include <pty.h> |
76 | 6f97dba0 | aliguori | |
77 | 6f97dba0 | aliguori | #include <linux/ppdev.h> |
78 | 6f97dba0 | aliguori | #include <linux/parport.h> |
79 | 6f97dba0 | aliguori | #endif
|
80 | 6f97dba0 | aliguori | #ifdef __sun__
|
81 | 6f97dba0 | aliguori | #include <sys/stat.h> |
82 | 6f97dba0 | aliguori | #include <sys/ethernet.h> |
83 | 6f97dba0 | aliguori | #include <sys/sockio.h> |
84 | 6f97dba0 | aliguori | #include <netinet/arp.h> |
85 | 6f97dba0 | aliguori | #include <netinet/in.h> |
86 | 6f97dba0 | aliguori | #include <netinet/in_systm.h> |
87 | 6f97dba0 | aliguori | #include <netinet/ip.h> |
88 | 6f97dba0 | aliguori | #include <netinet/ip_icmp.h> // must come after ip.h |
89 | 6f97dba0 | aliguori | #include <netinet/udp.h> |
90 | 6f97dba0 | aliguori | #include <netinet/tcp.h> |
91 | 6f97dba0 | aliguori | #include <net/if.h> |
92 | 6f97dba0 | aliguori | #include <syslog.h> |
93 | 6f97dba0 | aliguori | #include <stropts.h> |
94 | 6f97dba0 | aliguori | #endif
|
95 | 6f97dba0 | aliguori | #endif
|
96 | 6f97dba0 | aliguori | #endif
|
97 | 6f97dba0 | aliguori | |
98 | 6f97dba0 | aliguori | #include "qemu_socket.h" |
99 | 6f97dba0 | aliguori | |
100 | 9bd7854e | Amit Shah | #define READ_BUF_LEN 4096 |
101 | 9bd7854e | Amit Shah | |
102 | 6f97dba0 | aliguori | /***********************************************************/
|
103 | 6f97dba0 | aliguori | /* character device */
|
104 | 6f97dba0 | aliguori | |
105 | 72cf2d4f | Blue Swirl | static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
|
106 | 72cf2d4f | Blue Swirl | QTAILQ_HEAD_INITIALIZER(chardevs); |
107 | 2970a6c9 | aliguori | |
108 | 6f97dba0 | aliguori | static void qemu_chr_event(CharDriverState *s, int event) |
109 | 6f97dba0 | aliguori | { |
110 | 6f97dba0 | aliguori | if (!s->chr_event)
|
111 | 6f97dba0 | aliguori | return;
|
112 | 6f97dba0 | aliguori | s->chr_event(s->handler_opaque, event); |
113 | 6f97dba0 | aliguori | } |
114 | 6f97dba0 | aliguori | |
115 | 127338e6 | Amit Shah | static void qemu_chr_generic_open_bh(void *opaque) |
116 | 6f97dba0 | aliguori | { |
117 | 6f97dba0 | aliguori | CharDriverState *s = opaque; |
118 | f7cbc08f | Anthony Liguori | qemu_chr_event(s, CHR_EVENT_OPENED); |
119 | 6f97dba0 | aliguori | qemu_bh_delete(s->bh); |
120 | 6f97dba0 | aliguori | s->bh = NULL;
|
121 | 6f97dba0 | aliguori | } |
122 | 6f97dba0 | aliguori | |
123 | 127338e6 | Amit Shah | void qemu_chr_generic_open(CharDriverState *s)
|
124 | 6f97dba0 | aliguori | { |
125 | 69795d67 | Amit Shah | if (s->bh == NULL) { |
126 | 127338e6 | Amit Shah | s->bh = qemu_bh_new(qemu_chr_generic_open_bh, s); |
127 | 6f97dba0 | aliguori | qemu_bh_schedule(s->bh); |
128 | 6f97dba0 | aliguori | } |
129 | 6f97dba0 | aliguori | } |
130 | 6f97dba0 | aliguori | |
131 | 6f97dba0 | aliguori | int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len) |
132 | 6f97dba0 | aliguori | { |
133 | 6f97dba0 | aliguori | return s->chr_write(s, buf, len);
|
134 | 6f97dba0 | aliguori | } |
135 | 6f97dba0 | aliguori | |
136 | 6f97dba0 | aliguori | int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg) |
137 | 6f97dba0 | aliguori | { |
138 | 6f97dba0 | aliguori | if (!s->chr_ioctl)
|
139 | 6f97dba0 | aliguori | return -ENOTSUP;
|
140 | 6f97dba0 | aliguori | return s->chr_ioctl(s, cmd, arg);
|
141 | 6f97dba0 | aliguori | } |
142 | 6f97dba0 | aliguori | |
143 | 6f97dba0 | aliguori | int qemu_chr_can_read(CharDriverState *s)
|
144 | 6f97dba0 | aliguori | { |
145 | 6f97dba0 | aliguori | if (!s->chr_can_read)
|
146 | 6f97dba0 | aliguori | return 0; |
147 | 6f97dba0 | aliguori | return s->chr_can_read(s->handler_opaque);
|
148 | 6f97dba0 | aliguori | } |
149 | 6f97dba0 | aliguori | |
150 | 6f97dba0 | aliguori | void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len) |
151 | 6f97dba0 | aliguori | { |
152 | 6f97dba0 | aliguori | s->chr_read(s->handler_opaque, buf, len); |
153 | 6f97dba0 | aliguori | } |
154 | 6f97dba0 | aliguori | |
155 | 7d174059 | Mark McLoughlin | int qemu_chr_get_msgfd(CharDriverState *s)
|
156 | 7d174059 | Mark McLoughlin | { |
157 | 7d174059 | Mark McLoughlin | return s->get_msgfd ? s->get_msgfd(s) : -1; |
158 | 7d174059 | Mark McLoughlin | } |
159 | 7d174059 | Mark McLoughlin | |
160 | 6f97dba0 | aliguori | void qemu_chr_accept_input(CharDriverState *s)
|
161 | 6f97dba0 | aliguori | { |
162 | 6f97dba0 | aliguori | if (s->chr_accept_input)
|
163 | 6f97dba0 | aliguori | s->chr_accept_input(s); |
164 | 6f97dba0 | aliguori | } |
165 | 6f97dba0 | aliguori | |
166 | 6f97dba0 | aliguori | void qemu_chr_printf(CharDriverState *s, const char *fmt, ...) |
167 | 6f97dba0 | aliguori | { |
168 | 9bd7854e | Amit Shah | char buf[READ_BUF_LEN];
|
169 | 6f97dba0 | aliguori | va_list ap; |
170 | 6f97dba0 | aliguori | va_start(ap, fmt); |
171 | 6f97dba0 | aliguori | vsnprintf(buf, sizeof(buf), fmt, ap);
|
172 | 6f97dba0 | aliguori | qemu_chr_write(s, (uint8_t *)buf, strlen(buf)); |
173 | 6f97dba0 | aliguori | va_end(ap); |
174 | 6f97dba0 | aliguori | } |
175 | 6f97dba0 | aliguori | |
176 | 6f97dba0 | aliguori | void qemu_chr_send_event(CharDriverState *s, int event) |
177 | 6f97dba0 | aliguori | { |
178 | 6f97dba0 | aliguori | if (s->chr_send_event)
|
179 | 6f97dba0 | aliguori | s->chr_send_event(s, event); |
180 | 6f97dba0 | aliguori | } |
181 | 6f97dba0 | aliguori | |
182 | 6f97dba0 | aliguori | void qemu_chr_add_handlers(CharDriverState *s,
|
183 | 6f97dba0 | aliguori | IOCanRWHandler *fd_can_read, |
184 | 6f97dba0 | aliguori | IOReadHandler *fd_read, |
185 | 6f97dba0 | aliguori | IOEventHandler *fd_event, |
186 | 6f97dba0 | aliguori | void *opaque)
|
187 | 6f97dba0 | aliguori | { |
188 | 6f97dba0 | aliguori | s->chr_can_read = fd_can_read; |
189 | 6f97dba0 | aliguori | s->chr_read = fd_read; |
190 | 6f97dba0 | aliguori | s->chr_event = fd_event; |
191 | 6f97dba0 | aliguori | s->handler_opaque = opaque; |
192 | 6f97dba0 | aliguori | if (s->chr_update_read_handler)
|
193 | 6f97dba0 | aliguori | s->chr_update_read_handler(s); |
194 | 6f97dba0 | aliguori | } |
195 | 6f97dba0 | aliguori | |
196 | 6f97dba0 | aliguori | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
197 | 6f97dba0 | aliguori | { |
198 | 6f97dba0 | aliguori | return len;
|
199 | 6f97dba0 | aliguori | } |
200 | 6f97dba0 | aliguori | |
201 | 191bc01b | Gerd Hoffmann | static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
|
202 | 6f97dba0 | aliguori | { |
203 | 6f97dba0 | aliguori | CharDriverState *chr; |
204 | 6f97dba0 | aliguori | |
205 | 6f97dba0 | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
206 | 6f97dba0 | aliguori | chr->chr_write = null_chr_write; |
207 | 6f97dba0 | aliguori | return chr;
|
208 | 6f97dba0 | aliguori | } |
209 | 6f97dba0 | aliguori | |
210 | 6f97dba0 | aliguori | /* MUX driver for serial I/O splitting */
|
211 | 6f97dba0 | aliguori | #define MAX_MUX 4 |
212 | 6f97dba0 | aliguori | #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */ |
213 | 6f97dba0 | aliguori | #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1) |
214 | 6f97dba0 | aliguori | typedef struct { |
215 | 6f97dba0 | aliguori | IOCanRWHandler *chr_can_read[MAX_MUX]; |
216 | 6f97dba0 | aliguori | IOReadHandler *chr_read[MAX_MUX]; |
217 | 6f97dba0 | aliguori | IOEventHandler *chr_event[MAX_MUX]; |
218 | 6f97dba0 | aliguori | void *ext_opaque[MAX_MUX];
|
219 | 6f97dba0 | aliguori | CharDriverState *drv; |
220 | 799f1f23 | Gerd Hoffmann | int focus;
|
221 | 6f97dba0 | aliguori | int mux_cnt;
|
222 | 6f97dba0 | aliguori | int term_got_escape;
|
223 | 6f97dba0 | aliguori | int max_size;
|
224 | a80bf99f | aliguori | /* Intermediate input buffer allows to catch escape sequences even if the
|
225 | a80bf99f | aliguori | currently active device is not accepting any input - but only until it
|
226 | a80bf99f | aliguori | is full as well. */
|
227 | a80bf99f | aliguori | unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE]; |
228 | a80bf99f | aliguori | int prod[MAX_MUX];
|
229 | a80bf99f | aliguori | int cons[MAX_MUX];
|
230 | 2d22959d | Jan Kiszka | int timestamps;
|
231 | 4ab312f7 | Jan Kiszka | int linestart;
|
232 | 2d22959d | Jan Kiszka | int64_t timestamps_start; |
233 | 6f97dba0 | aliguori | } MuxDriver; |
234 | 6f97dba0 | aliguori | |
235 | 6f97dba0 | aliguori | |
236 | 6f97dba0 | aliguori | static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
237 | 6f97dba0 | aliguori | { |
238 | 6f97dba0 | aliguori | MuxDriver *d = chr->opaque; |
239 | 6f97dba0 | aliguori | int ret;
|
240 | 2d22959d | Jan Kiszka | if (!d->timestamps) {
|
241 | 6f97dba0 | aliguori | ret = d->drv->chr_write(d->drv, buf, len); |
242 | 6f97dba0 | aliguori | } else {
|
243 | 6f97dba0 | aliguori | int i;
|
244 | 6f97dba0 | aliguori | |
245 | 6f97dba0 | aliguori | ret = 0;
|
246 | 4ab312f7 | Jan Kiszka | for (i = 0; i < len; i++) { |
247 | 4ab312f7 | Jan Kiszka | if (d->linestart) {
|
248 | 6f97dba0 | aliguori | char buf1[64]; |
249 | 6f97dba0 | aliguori | int64_t ti; |
250 | 6f97dba0 | aliguori | int secs;
|
251 | 6f97dba0 | aliguori | |
252 | 6f97dba0 | aliguori | ti = qemu_get_clock(rt_clock); |
253 | 2d22959d | Jan Kiszka | if (d->timestamps_start == -1) |
254 | 2d22959d | Jan Kiszka | d->timestamps_start = ti; |
255 | 2d22959d | Jan Kiszka | ti -= d->timestamps_start; |
256 | a4bb1db8 | aliguori | secs = ti / 1000;
|
257 | 6f97dba0 | aliguori | snprintf(buf1, sizeof(buf1),
|
258 | 6f97dba0 | aliguori | "[%02d:%02d:%02d.%03d] ",
|
259 | 6f97dba0 | aliguori | secs / 3600,
|
260 | 6f97dba0 | aliguori | (secs / 60) % 60, |
261 | 6f97dba0 | aliguori | secs % 60,
|
262 | a4bb1db8 | aliguori | (int)(ti % 1000)); |
263 | 6f97dba0 | aliguori | d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1)); |
264 | 4ab312f7 | Jan Kiszka | d->linestart = 0;
|
265 | 4ab312f7 | Jan Kiszka | } |
266 | 4ab312f7 | Jan Kiszka | ret += d->drv->chr_write(d->drv, buf+i, 1);
|
267 | 4ab312f7 | Jan Kiszka | if (buf[i] == '\n') { |
268 | 4ab312f7 | Jan Kiszka | d->linestart = 1;
|
269 | 6f97dba0 | aliguori | } |
270 | 6f97dba0 | aliguori | } |
271 | 6f97dba0 | aliguori | } |
272 | 6f97dba0 | aliguori | return ret;
|
273 | 6f97dba0 | aliguori | } |
274 | 6f97dba0 | aliguori | |
275 | 6f97dba0 | aliguori | static const char * const mux_help[] = { |
276 | 6f97dba0 | aliguori | "% h print this help\n\r",
|
277 | 6f97dba0 | aliguori | "% x exit emulator\n\r",
|
278 | 6f97dba0 | aliguori | "% s save disk data back to file (if -snapshot)\n\r",
|
279 | 6f97dba0 | aliguori | "% t toggle console timestamps\n\r"
|
280 | 6f97dba0 | aliguori | "% b send break (magic sysrq)\n\r",
|
281 | 6f97dba0 | aliguori | "% c switch between console and monitor\n\r",
|
282 | 6f97dba0 | aliguori | "% % sends %\n\r",
|
283 | 6f97dba0 | aliguori | NULL
|
284 | 6f97dba0 | aliguori | }; |
285 | 6f97dba0 | aliguori | |
286 | 6f97dba0 | aliguori | int term_escape_char = 0x01; /* ctrl-a is used for escape */ |
287 | 6f97dba0 | aliguori | static void mux_print_help(CharDriverState *chr) |
288 | 6f97dba0 | aliguori | { |
289 | 6f97dba0 | aliguori | int i, j;
|
290 | 6f97dba0 | aliguori | char ebuf[15] = "Escape-Char"; |
291 | 6f97dba0 | aliguori | char cbuf[50] = "\n\r"; |
292 | 6f97dba0 | aliguori | |
293 | 6f97dba0 | aliguori | if (term_escape_char > 0 && term_escape_char < 26) { |
294 | 6f97dba0 | aliguori | snprintf(cbuf, sizeof(cbuf), "\n\r"); |
295 | 6f97dba0 | aliguori | snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a'); |
296 | 6f97dba0 | aliguori | } else {
|
297 | 6f97dba0 | aliguori | snprintf(cbuf, sizeof(cbuf),
|
298 | 6f97dba0 | aliguori | "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
|
299 | 6f97dba0 | aliguori | term_escape_char); |
300 | 6f97dba0 | aliguori | } |
301 | 6f97dba0 | aliguori | chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf)); |
302 | 6f97dba0 | aliguori | for (i = 0; mux_help[i] != NULL; i++) { |
303 | 6f97dba0 | aliguori | for (j=0; mux_help[i][j] != '\0'; j++) { |
304 | 6f97dba0 | aliguori | if (mux_help[i][j] == '%') |
305 | 6f97dba0 | aliguori | chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf)); |
306 | 6f97dba0 | aliguori | else
|
307 | 6f97dba0 | aliguori | chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
|
308 | 6f97dba0 | aliguori | } |
309 | 6f97dba0 | aliguori | } |
310 | 6f97dba0 | aliguori | } |
311 | 6f97dba0 | aliguori | |
312 | 2724b180 | aliguori | static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event) |
313 | 2724b180 | aliguori | { |
314 | 2724b180 | aliguori | if (d->chr_event[mux_nr])
|
315 | 2724b180 | aliguori | d->chr_event[mux_nr](d->ext_opaque[mux_nr], event); |
316 | 2724b180 | aliguori | } |
317 | 2724b180 | aliguori | |
318 | 6f97dba0 | aliguori | static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch) |
319 | 6f97dba0 | aliguori | { |
320 | 6f97dba0 | aliguori | if (d->term_got_escape) {
|
321 | 6f97dba0 | aliguori | d->term_got_escape = 0;
|
322 | 6f97dba0 | aliguori | if (ch == term_escape_char)
|
323 | 6f97dba0 | aliguori | goto send_char;
|
324 | 6f97dba0 | aliguori | switch(ch) {
|
325 | 6f97dba0 | aliguori | case '?': |
326 | 6f97dba0 | aliguori | case 'h': |
327 | 6f97dba0 | aliguori | mux_print_help(chr); |
328 | 6f97dba0 | aliguori | break;
|
329 | 6f97dba0 | aliguori | case 'x': |
330 | 6f97dba0 | aliguori | { |
331 | 6f97dba0 | aliguori | const char *term = "QEMU: Terminated\n\r"; |
332 | 6f97dba0 | aliguori | chr->chr_write(chr,(uint8_t *)term,strlen(term)); |
333 | 6f97dba0 | aliguori | exit(0);
|
334 | 6f97dba0 | aliguori | break;
|
335 | 6f97dba0 | aliguori | } |
336 | 6f97dba0 | aliguori | case 's': |
337 | 6f97dba0 | aliguori | { |
338 | 751c6a17 | Gerd Hoffmann | DriveInfo *dinfo; |
339 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(dinfo, &drives, next) { |
340 | 751c6a17 | Gerd Hoffmann | bdrv_commit(dinfo->bdrv); |
341 | 6f97dba0 | aliguori | } |
342 | 6f97dba0 | aliguori | } |
343 | 6f97dba0 | aliguori | break;
|
344 | 6f97dba0 | aliguori | case 'b': |
345 | 6f97dba0 | aliguori | qemu_chr_event(chr, CHR_EVENT_BREAK); |
346 | 6f97dba0 | aliguori | break;
|
347 | 6f97dba0 | aliguori | case 'c': |
348 | 6f97dba0 | aliguori | /* Switch to the next registered device */
|
349 | 799f1f23 | Gerd Hoffmann | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); |
350 | 799f1f23 | Gerd Hoffmann | d->focus++; |
351 | 799f1f23 | Gerd Hoffmann | if (d->focus >= d->mux_cnt)
|
352 | 799f1f23 | Gerd Hoffmann | d->focus = 0;
|
353 | 799f1f23 | Gerd Hoffmann | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN); |
354 | 6f97dba0 | aliguori | break;
|
355 | 2d22959d | Jan Kiszka | case 't': |
356 | 2d22959d | Jan Kiszka | d->timestamps = !d->timestamps; |
357 | 2d22959d | Jan Kiszka | d->timestamps_start = -1;
|
358 | 4ab312f7 | Jan Kiszka | d->linestart = 0;
|
359 | 2d22959d | Jan Kiszka | break;
|
360 | 6f97dba0 | aliguori | } |
361 | 6f97dba0 | aliguori | } else if (ch == term_escape_char) { |
362 | 6f97dba0 | aliguori | d->term_got_escape = 1;
|
363 | 6f97dba0 | aliguori | } else {
|
364 | 6f97dba0 | aliguori | send_char:
|
365 | 6f97dba0 | aliguori | return 1; |
366 | 6f97dba0 | aliguori | } |
367 | 6f97dba0 | aliguori | return 0; |
368 | 6f97dba0 | aliguori | } |
369 | 6f97dba0 | aliguori | |
370 | 6f97dba0 | aliguori | static void mux_chr_accept_input(CharDriverState *chr) |
371 | 6f97dba0 | aliguori | { |
372 | 6f97dba0 | aliguori | MuxDriver *d = chr->opaque; |
373 | 799f1f23 | Gerd Hoffmann | int m = d->focus;
|
374 | 6f97dba0 | aliguori | |
375 | a80bf99f | aliguori | while (d->prod[m] != d->cons[m] &&
|
376 | 6f97dba0 | aliguori | d->chr_can_read[m] && |
377 | 6f97dba0 | aliguori | d->chr_can_read[m](d->ext_opaque[m])) { |
378 | 6f97dba0 | aliguori | d->chr_read[m](d->ext_opaque[m], |
379 | a80bf99f | aliguori | &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
|
380 | 6f97dba0 | aliguori | } |
381 | 6f97dba0 | aliguori | } |
382 | 6f97dba0 | aliguori | |
383 | 6f97dba0 | aliguori | static int mux_chr_can_read(void *opaque) |
384 | 6f97dba0 | aliguori | { |
385 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
386 | 6f97dba0 | aliguori | MuxDriver *d = chr->opaque; |
387 | 799f1f23 | Gerd Hoffmann | int m = d->focus;
|
388 | 6f97dba0 | aliguori | |
389 | a80bf99f | aliguori | if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
|
390 | 6f97dba0 | aliguori | return 1; |
391 | a80bf99f | aliguori | if (d->chr_can_read[m])
|
392 | a80bf99f | aliguori | return d->chr_can_read[m](d->ext_opaque[m]);
|
393 | 6f97dba0 | aliguori | return 0; |
394 | 6f97dba0 | aliguori | } |
395 | 6f97dba0 | aliguori | |
396 | 6f97dba0 | aliguori | static void mux_chr_read(void *opaque, const uint8_t *buf, int size) |
397 | 6f97dba0 | aliguori | { |
398 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
399 | 6f97dba0 | aliguori | MuxDriver *d = chr->opaque; |
400 | 799f1f23 | Gerd Hoffmann | int m = d->focus;
|
401 | 6f97dba0 | aliguori | int i;
|
402 | 6f97dba0 | aliguori | |
403 | 6f97dba0 | aliguori | mux_chr_accept_input (opaque); |
404 | 6f97dba0 | aliguori | |
405 | 6f97dba0 | aliguori | for(i = 0; i < size; i++) |
406 | 6f97dba0 | aliguori | if (mux_proc_byte(chr, d, buf[i])) {
|
407 | a80bf99f | aliguori | if (d->prod[m] == d->cons[m] &&
|
408 | 6f97dba0 | aliguori | d->chr_can_read[m] && |
409 | 6f97dba0 | aliguori | d->chr_can_read[m](d->ext_opaque[m])) |
410 | 6f97dba0 | aliguori | d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
|
411 | 6f97dba0 | aliguori | else
|
412 | a80bf99f | aliguori | d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i]; |
413 | 6f97dba0 | aliguori | } |
414 | 6f97dba0 | aliguori | } |
415 | 6f97dba0 | aliguori | |
416 | 6f97dba0 | aliguori | static void mux_chr_event(void *opaque, int event) |
417 | 6f97dba0 | aliguori | { |
418 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
419 | 6f97dba0 | aliguori | MuxDriver *d = chr->opaque; |
420 | 6f97dba0 | aliguori | int i;
|
421 | 6f97dba0 | aliguori | |
422 | 6f97dba0 | aliguori | /* Send the event to all registered listeners */
|
423 | 6f97dba0 | aliguori | for (i = 0; i < d->mux_cnt; i++) |
424 | 2724b180 | aliguori | mux_chr_send_event(d, i, event); |
425 | 6f97dba0 | aliguori | } |
426 | 6f97dba0 | aliguori | |
427 | 6f97dba0 | aliguori | static void mux_chr_update_read_handler(CharDriverState *chr) |
428 | 6f97dba0 | aliguori | { |
429 | 6f97dba0 | aliguori | MuxDriver *d = chr->opaque; |
430 | 6f97dba0 | aliguori | |
431 | 6f97dba0 | aliguori | if (d->mux_cnt >= MAX_MUX) {
|
432 | 6f97dba0 | aliguori | fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
|
433 | 6f97dba0 | aliguori | return;
|
434 | 6f97dba0 | aliguori | } |
435 | 6f97dba0 | aliguori | d->ext_opaque[d->mux_cnt] = chr->handler_opaque; |
436 | 6f97dba0 | aliguori | d->chr_can_read[d->mux_cnt] = chr->chr_can_read; |
437 | 6f97dba0 | aliguori | d->chr_read[d->mux_cnt] = chr->chr_read; |
438 | 6f97dba0 | aliguori | d->chr_event[d->mux_cnt] = chr->chr_event; |
439 | 6f97dba0 | aliguori | /* Fix up the real driver with mux routines */
|
440 | 6f97dba0 | aliguori | if (d->mux_cnt == 0) { |
441 | 6f97dba0 | aliguori | qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read, |
442 | 6f97dba0 | aliguori | mux_chr_event, chr); |
443 | 6f97dba0 | aliguori | } |
444 | 799f1f23 | Gerd Hoffmann | if (d->focus != -1) { |
445 | 799f1f23 | Gerd Hoffmann | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); |
446 | a7aec5da | Gerd Hoffmann | } |
447 | 799f1f23 | Gerd Hoffmann | d->focus = d->mux_cnt; |
448 | 6f97dba0 | aliguori | d->mux_cnt++; |
449 | 799f1f23 | Gerd Hoffmann | mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN); |
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 | 799f1f23 | Gerd Hoffmann | d->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 | 9bd7854e | Amit Shah | uint8_t buf[READ_BUF_LEN]; |
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 | 793cbfb5 | Amit Shah | qemu_chr_event(chr, CHR_EVENT_CLOSED); |
563 | 6f97dba0 | aliguori | return;
|
564 | 6f97dba0 | aliguori | } |
565 | 6f97dba0 | aliguori | if (size > 0) { |
566 | 6f97dba0 | aliguori | qemu_chr_read(chr, buf, size); |
567 | 6f97dba0 | aliguori | } |
568 | 6f97dba0 | aliguori | } |
569 | 6f97dba0 | aliguori | |
570 | 6f97dba0 | aliguori | static void fd_chr_update_read_handler(CharDriverState *chr) |
571 | 6f97dba0 | aliguori | { |
572 | 6f97dba0 | aliguori | FDCharDriver *s = chr->opaque; |
573 | 6f97dba0 | aliguori | |
574 | 6f97dba0 | aliguori | if (s->fd_in >= 0) { |
575 | 993fbfdb | Anthony Liguori | if (display_type == DT_NOGRAPHIC && s->fd_in == 0) { |
576 | 6f97dba0 | aliguori | } else {
|
577 | 6f97dba0 | aliguori | qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll, |
578 | 6f97dba0 | aliguori | fd_chr_read, NULL, chr);
|
579 | 6f97dba0 | aliguori | } |
580 | 6f97dba0 | aliguori | } |
581 | 6f97dba0 | aliguori | } |
582 | 6f97dba0 | aliguori | |
583 | 6f97dba0 | aliguori | static void fd_chr_close(struct CharDriverState *chr) |
584 | 6f97dba0 | aliguori | { |
585 | 6f97dba0 | aliguori | FDCharDriver *s = chr->opaque; |
586 | 6f97dba0 | aliguori | |
587 | 6f97dba0 | aliguori | if (s->fd_in >= 0) { |
588 | 993fbfdb | Anthony Liguori | if (display_type == DT_NOGRAPHIC && s->fd_in == 0) { |
589 | 6f97dba0 | aliguori | } else {
|
590 | 6f97dba0 | aliguori | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
591 | 6f97dba0 | aliguori | } |
592 | 6f97dba0 | aliguori | } |
593 | 6f97dba0 | aliguori | |
594 | 6f97dba0 | aliguori | qemu_free(s); |
595 | 793cbfb5 | Amit Shah | qemu_chr_event(chr, CHR_EVENT_CLOSED); |
596 | 6f97dba0 | aliguori | } |
597 | 6f97dba0 | aliguori | |
598 | 6f97dba0 | aliguori | /* open a character device to a unix fd */
|
599 | 6f97dba0 | aliguori | static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) |
600 | 6f97dba0 | aliguori | { |
601 | 6f97dba0 | aliguori | CharDriverState *chr; |
602 | 6f97dba0 | aliguori | FDCharDriver *s; |
603 | 6f97dba0 | aliguori | |
604 | 6f97dba0 | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
605 | 6f97dba0 | aliguori | s = qemu_mallocz(sizeof(FDCharDriver));
|
606 | 6f97dba0 | aliguori | s->fd_in = fd_in; |
607 | 6f97dba0 | aliguori | s->fd_out = fd_out; |
608 | 6f97dba0 | aliguori | chr->opaque = s; |
609 | 6f97dba0 | aliguori | chr->chr_write = fd_chr_write; |
610 | 6f97dba0 | aliguori | chr->chr_update_read_handler = fd_chr_update_read_handler; |
611 | 6f97dba0 | aliguori | chr->chr_close = fd_chr_close; |
612 | 6f97dba0 | aliguori | |
613 | 127338e6 | Amit Shah | qemu_chr_generic_open(chr); |
614 | 6f97dba0 | aliguori | |
615 | 6f97dba0 | aliguori | return chr;
|
616 | 6f97dba0 | aliguori | } |
617 | 6f97dba0 | aliguori | |
618 | 7d31544f | Gerd Hoffmann | static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
|
619 | 6f97dba0 | aliguori | { |
620 | 6f97dba0 | aliguori | int fd_out;
|
621 | 6f97dba0 | aliguori | |
622 | 7d31544f | Gerd Hoffmann | TFR(fd_out = open(qemu_opt_get(opts, "path"),
|
623 | 7d31544f | Gerd Hoffmann | O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
|
624 | 6f97dba0 | aliguori | if (fd_out < 0) |
625 | 6f97dba0 | aliguori | return NULL; |
626 | 6f97dba0 | aliguori | return qemu_chr_open_fd(-1, fd_out); |
627 | 6f97dba0 | aliguori | } |
628 | 6f97dba0 | aliguori | |
629 | 7d31544f | Gerd Hoffmann | static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
|
630 | 6f97dba0 | aliguori | { |
631 | 6f97dba0 | aliguori | int fd_in, fd_out;
|
632 | 6f97dba0 | aliguori | char filename_in[256], filename_out[256]; |
633 | 7d31544f | Gerd Hoffmann | const char *filename = qemu_opt_get(opts, "path"); |
634 | 7d31544f | Gerd Hoffmann | |
635 | 7d31544f | Gerd Hoffmann | if (filename == NULL) { |
636 | 7d31544f | Gerd Hoffmann | fprintf(stderr, "chardev: pipe: no filename given\n");
|
637 | 7d31544f | Gerd Hoffmann | return NULL; |
638 | 7d31544f | Gerd Hoffmann | } |
639 | 6f97dba0 | aliguori | |
640 | 6f97dba0 | aliguori | snprintf(filename_in, 256, "%s.in", filename); |
641 | 6f97dba0 | aliguori | snprintf(filename_out, 256, "%s.out", filename); |
642 | 6f97dba0 | aliguori | TFR(fd_in = open(filename_in, O_RDWR | O_BINARY)); |
643 | 6f97dba0 | aliguori | TFR(fd_out = open(filename_out, O_RDWR | O_BINARY)); |
644 | 6f97dba0 | aliguori | if (fd_in < 0 || fd_out < 0) { |
645 | 6f97dba0 | aliguori | if (fd_in >= 0) |
646 | 6f97dba0 | aliguori | close(fd_in); |
647 | 6f97dba0 | aliguori | if (fd_out >= 0) |
648 | 6f97dba0 | aliguori | close(fd_out); |
649 | 6f97dba0 | aliguori | TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY)); |
650 | 6f97dba0 | aliguori | if (fd_in < 0) |
651 | 6f97dba0 | aliguori | return NULL; |
652 | 6f97dba0 | aliguori | } |
653 | 6f97dba0 | aliguori | return qemu_chr_open_fd(fd_in, fd_out);
|
654 | 6f97dba0 | aliguori | } |
655 | 6f97dba0 | aliguori | |
656 | 6f97dba0 | aliguori | |
657 | 6f97dba0 | aliguori | /* for STDIO, we handle the case where several clients use it
|
658 | 6f97dba0 | aliguori | (nographic mode) */
|
659 | 6f97dba0 | aliguori | |
660 | 6f97dba0 | aliguori | #define TERM_FIFO_MAX_SIZE 1 |
661 | 6f97dba0 | aliguori | |
662 | 6f97dba0 | aliguori | static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
|
663 | 6f97dba0 | aliguori | static int term_fifo_size; |
664 | 6f97dba0 | aliguori | |
665 | 6f97dba0 | aliguori | static int stdio_read_poll(void *opaque) |
666 | 6f97dba0 | aliguori | { |
667 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
668 | 6f97dba0 | aliguori | |
669 | 6f97dba0 | aliguori | /* try to flush the queue if needed */
|
670 | 6f97dba0 | aliguori | if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) { |
671 | 6f97dba0 | aliguori | qemu_chr_read(chr, term_fifo, 1);
|
672 | 6f97dba0 | aliguori | term_fifo_size = 0;
|
673 | 6f97dba0 | aliguori | } |
674 | 6f97dba0 | aliguori | /* see if we can absorb more chars */
|
675 | 6f97dba0 | aliguori | if (term_fifo_size == 0) |
676 | 6f97dba0 | aliguori | return 1; |
677 | 6f97dba0 | aliguori | else
|
678 | 6f97dba0 | aliguori | return 0; |
679 | 6f97dba0 | aliguori | } |
680 | 6f97dba0 | aliguori | |
681 | 6f97dba0 | aliguori | static void stdio_read(void *opaque) |
682 | 6f97dba0 | aliguori | { |
683 | 6f97dba0 | aliguori | int size;
|
684 | 6f97dba0 | aliguori | uint8_t buf[1];
|
685 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
686 | 6f97dba0 | aliguori | |
687 | 6f97dba0 | aliguori | size = read(0, buf, 1); |
688 | 6f97dba0 | aliguori | if (size == 0) { |
689 | 6f97dba0 | aliguori | /* stdin has been closed. Remove it from the active list. */
|
690 | 6f97dba0 | aliguori | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
691 | 793cbfb5 | Amit Shah | qemu_chr_event(chr, CHR_EVENT_CLOSED); |
692 | 6f97dba0 | aliguori | return;
|
693 | 6f97dba0 | aliguori | } |
694 | 6f97dba0 | aliguori | if (size > 0) { |
695 | 6f97dba0 | aliguori | if (qemu_chr_can_read(chr) > 0) { |
696 | 6f97dba0 | aliguori | qemu_chr_read(chr, buf, 1);
|
697 | 6f97dba0 | aliguori | } else if (term_fifo_size == 0) { |
698 | 6f97dba0 | aliguori | term_fifo[term_fifo_size++] = buf[0];
|
699 | 6f97dba0 | aliguori | } |
700 | 6f97dba0 | aliguori | } |
701 | 6f97dba0 | aliguori | } |
702 | 6f97dba0 | aliguori | |
703 | 6f97dba0 | aliguori | /* init terminal so that we can grab keys */
|
704 | 6f97dba0 | aliguori | static struct termios oldtty; |
705 | 6f97dba0 | aliguori | static int old_fd0_flags; |
706 | 6f97dba0 | aliguori | static int term_atexit_done; |
707 | 6f97dba0 | aliguori | |
708 | 6f97dba0 | aliguori | static void term_exit(void) |
709 | 6f97dba0 | aliguori | { |
710 | 6f97dba0 | aliguori | tcsetattr (0, TCSANOW, &oldtty);
|
711 | 6f97dba0 | aliguori | fcntl(0, F_SETFL, old_fd0_flags);
|
712 | 6f97dba0 | aliguori | } |
713 | 6f97dba0 | aliguori | |
714 | 5989020b | Kusanagi Kouichi | static void term_init(QemuOpts *opts) |
715 | 6f97dba0 | aliguori | { |
716 | 6f97dba0 | aliguori | struct termios tty;
|
717 | 6f97dba0 | aliguori | |
718 | 6f97dba0 | aliguori | tcgetattr (0, &tty);
|
719 | 6f97dba0 | aliguori | oldtty = tty; |
720 | 6f97dba0 | aliguori | old_fd0_flags = fcntl(0, F_GETFL);
|
721 | 6f97dba0 | aliguori | |
722 | 6f97dba0 | aliguori | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
723 | 6f97dba0 | aliguori | |INLCR|IGNCR|ICRNL|IXON); |
724 | 6f97dba0 | aliguori | tty.c_oflag |= OPOST; |
725 | 6f97dba0 | aliguori | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
726 | 6f97dba0 | aliguori | /* if graphical mode, we allow Ctrl-C handling */
|
727 | 5989020b | Kusanagi Kouichi | if (!qemu_opt_get_bool(opts, "signal", display_type != DT_NOGRAPHIC)) |
728 | 6f97dba0 | aliguori | tty.c_lflag &= ~ISIG; |
729 | 6f97dba0 | aliguori | tty.c_cflag &= ~(CSIZE|PARENB); |
730 | 6f97dba0 | aliguori | tty.c_cflag |= CS8; |
731 | 6f97dba0 | aliguori | tty.c_cc[VMIN] = 1;
|
732 | 6f97dba0 | aliguori | tty.c_cc[VTIME] = 0;
|
733 | 6f97dba0 | aliguori | |
734 | 6f97dba0 | aliguori | tcsetattr (0, TCSANOW, &tty);
|
735 | 6f97dba0 | aliguori | |
736 | 6f97dba0 | aliguori | if (!term_atexit_done++)
|
737 | 6f97dba0 | aliguori | atexit(term_exit); |
738 | 6f97dba0 | aliguori | |
739 | 6f97dba0 | aliguori | fcntl(0, F_SETFL, O_NONBLOCK);
|
740 | 6f97dba0 | aliguori | } |
741 | 6f97dba0 | aliguori | |
742 | 6f97dba0 | aliguori | static void qemu_chr_close_stdio(struct CharDriverState *chr) |
743 | 6f97dba0 | aliguori | { |
744 | 6f97dba0 | aliguori | term_exit(); |
745 | 6f97dba0 | aliguori | stdio_nb_clients--; |
746 | 6f97dba0 | aliguori | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
747 | 6f97dba0 | aliguori | fd_chr_close(chr); |
748 | 6f97dba0 | aliguori | } |
749 | 6f97dba0 | aliguori | |
750 | 3c17affb | Gerd Hoffmann | static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
|
751 | 6f97dba0 | aliguori | { |
752 | 6f97dba0 | aliguori | CharDriverState *chr; |
753 | 6f97dba0 | aliguori | |
754 | 6f97dba0 | aliguori | if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
|
755 | 6f97dba0 | aliguori | return NULL; |
756 | 6f97dba0 | aliguori | chr = qemu_chr_open_fd(0, 1); |
757 | 6f97dba0 | aliguori | chr->chr_close = qemu_chr_close_stdio; |
758 | 6f97dba0 | aliguori | qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr); |
759 | 6f97dba0 | aliguori | stdio_nb_clients++; |
760 | 5989020b | Kusanagi Kouichi | term_init(opts); |
761 | 6f97dba0 | aliguori | |
762 | 6f97dba0 | aliguori | return chr;
|
763 | 6f97dba0 | aliguori | } |
764 | 6f97dba0 | aliguori | |
765 | 6f97dba0 | aliguori | #ifdef __sun__
|
766 | 6f97dba0 | aliguori | /* Once Solaris has openpty(), this is going to be removed. */
|
767 | 3f4cb3d3 | blueswir1 | static int openpty(int *amaster, int *aslave, char *name, |
768 | 3f4cb3d3 | blueswir1 | struct termios *termp, struct winsize *winp) |
769 | 6f97dba0 | aliguori | { |
770 | 6f97dba0 | aliguori | const char *slave; |
771 | 6f97dba0 | aliguori | int mfd = -1, sfd = -1; |
772 | 6f97dba0 | aliguori | |
773 | 6f97dba0 | aliguori | *amaster = *aslave = -1;
|
774 | 6f97dba0 | aliguori | |
775 | 6f97dba0 | aliguori | mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
|
776 | 6f97dba0 | aliguori | if (mfd < 0) |
777 | 6f97dba0 | aliguori | goto err;
|
778 | 6f97dba0 | aliguori | |
779 | 6f97dba0 | aliguori | if (grantpt(mfd) == -1 || unlockpt(mfd) == -1) |
780 | 6f97dba0 | aliguori | goto err;
|
781 | 6f97dba0 | aliguori | |
782 | 6f97dba0 | aliguori | if ((slave = ptsname(mfd)) == NULL) |
783 | 6f97dba0 | aliguori | goto err;
|
784 | 6f97dba0 | aliguori | |
785 | 6f97dba0 | aliguori | if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1) |
786 | 6f97dba0 | aliguori | goto err;
|
787 | 6f97dba0 | aliguori | |
788 | 6f97dba0 | aliguori | if (ioctl(sfd, I_PUSH, "ptem") == -1 || |
789 | 6f97dba0 | aliguori | (termp != NULL && tcgetattr(sfd, termp) < 0)) |
790 | 6f97dba0 | aliguori | goto err;
|
791 | 6f97dba0 | aliguori | |
792 | 6f97dba0 | aliguori | if (amaster)
|
793 | 6f97dba0 | aliguori | *amaster = mfd; |
794 | 6f97dba0 | aliguori | if (aslave)
|
795 | 6f97dba0 | aliguori | *aslave = sfd; |
796 | 6f97dba0 | aliguori | if (winp)
|
797 | 6f97dba0 | aliguori | ioctl(sfd, TIOCSWINSZ, winp); |
798 | 6f97dba0 | aliguori | |
799 | 6f97dba0 | aliguori | return 0; |
800 | 6f97dba0 | aliguori | |
801 | 6f97dba0 | aliguori | err:
|
802 | 6f97dba0 | aliguori | if (sfd != -1) |
803 | 6f97dba0 | aliguori | close(sfd); |
804 | 6f97dba0 | aliguori | close(mfd); |
805 | 6f97dba0 | aliguori | return -1; |
806 | 6f97dba0 | aliguori | } |
807 | 6f97dba0 | aliguori | |
808 | 3f4cb3d3 | blueswir1 | static void cfmakeraw (struct termios *termios_p) |
809 | 6f97dba0 | aliguori | { |
810 | 6f97dba0 | aliguori | termios_p->c_iflag &= |
811 | 6f97dba0 | aliguori | ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); |
812 | 6f97dba0 | aliguori | termios_p->c_oflag &= ~OPOST; |
813 | 6f97dba0 | aliguori | termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); |
814 | 6f97dba0 | aliguori | termios_p->c_cflag &= ~(CSIZE|PARENB); |
815 | 6f97dba0 | aliguori | termios_p->c_cflag |= CS8; |
816 | 6f97dba0 | aliguori | |
817 | 6f97dba0 | aliguori | termios_p->c_cc[VMIN] = 0;
|
818 | 6f97dba0 | aliguori | termios_p->c_cc[VTIME] = 0;
|
819 | 6f97dba0 | aliguori | } |
820 | 6f97dba0 | aliguori | #endif
|
821 | 6f97dba0 | aliguori | |
822 | 6f97dba0 | aliguori | #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
|
823 | c5e97233 | blueswir1 | || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) |
824 | 6f97dba0 | aliguori | |
825 | 6f97dba0 | aliguori | typedef struct { |
826 | 6f97dba0 | aliguori | int fd;
|
827 | 6f97dba0 | aliguori | int connected;
|
828 | 6f97dba0 | aliguori | int polling;
|
829 | 6f97dba0 | aliguori | int read_bytes;
|
830 | 6f97dba0 | aliguori | QEMUTimer *timer; |
831 | 6f97dba0 | aliguori | } PtyCharDriver; |
832 | 6f97dba0 | aliguori | |
833 | 6f97dba0 | aliguori | static void pty_chr_update_read_handler(CharDriverState *chr); |
834 | 6f97dba0 | aliguori | static void pty_chr_state(CharDriverState *chr, int connected); |
835 | 6f97dba0 | aliguori | |
836 | 6f97dba0 | aliguori | static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
837 | 6f97dba0 | aliguori | { |
838 | 6f97dba0 | aliguori | PtyCharDriver *s = chr->opaque; |
839 | 6f97dba0 | aliguori | |
840 | 6f97dba0 | aliguori | if (!s->connected) {
|
841 | 6f97dba0 | aliguori | /* guest sends data, check for (re-)connect */
|
842 | 6f97dba0 | aliguori | pty_chr_update_read_handler(chr); |
843 | 6f97dba0 | aliguori | return 0; |
844 | 6f97dba0 | aliguori | } |
845 | 6f97dba0 | aliguori | return send_all(s->fd, buf, len);
|
846 | 6f97dba0 | aliguori | } |
847 | 6f97dba0 | aliguori | |
848 | 6f97dba0 | aliguori | static int pty_chr_read_poll(void *opaque) |
849 | 6f97dba0 | aliguori | { |
850 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
851 | 6f97dba0 | aliguori | PtyCharDriver *s = chr->opaque; |
852 | 6f97dba0 | aliguori | |
853 | 6f97dba0 | aliguori | s->read_bytes = qemu_chr_can_read(chr); |
854 | 6f97dba0 | aliguori | return s->read_bytes;
|
855 | 6f97dba0 | aliguori | } |
856 | 6f97dba0 | aliguori | |
857 | 6f97dba0 | aliguori | static void pty_chr_read(void *opaque) |
858 | 6f97dba0 | aliguori | { |
859 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
860 | 6f97dba0 | aliguori | PtyCharDriver *s = chr->opaque; |
861 | 6f97dba0 | aliguori | int size, len;
|
862 | 9bd7854e | Amit Shah | uint8_t buf[READ_BUF_LEN]; |
863 | 6f97dba0 | aliguori | |
864 | 6f97dba0 | aliguori | len = sizeof(buf);
|
865 | 6f97dba0 | aliguori | if (len > s->read_bytes)
|
866 | 6f97dba0 | aliguori | len = s->read_bytes; |
867 | 6f97dba0 | aliguori | if (len == 0) |
868 | 6f97dba0 | aliguori | return;
|
869 | 6f97dba0 | aliguori | size = read(s->fd, buf, len); |
870 | 6f97dba0 | aliguori | if ((size == -1 && errno == EIO) || |
871 | 6f97dba0 | aliguori | (size == 0)) {
|
872 | 6f97dba0 | aliguori | pty_chr_state(chr, 0);
|
873 | 6f97dba0 | aliguori | return;
|
874 | 6f97dba0 | aliguori | } |
875 | 6f97dba0 | aliguori | if (size > 0) { |
876 | 6f97dba0 | aliguori | pty_chr_state(chr, 1);
|
877 | 6f97dba0 | aliguori | qemu_chr_read(chr, buf, size); |
878 | 6f97dba0 | aliguori | } |
879 | 6f97dba0 | aliguori | } |
880 | 6f97dba0 | aliguori | |
881 | 6f97dba0 | aliguori | static void pty_chr_update_read_handler(CharDriverState *chr) |
882 | 6f97dba0 | aliguori | { |
883 | 6f97dba0 | aliguori | PtyCharDriver *s = chr->opaque; |
884 | 6f97dba0 | aliguori | |
885 | 6f97dba0 | aliguori | qemu_set_fd_handler2(s->fd, pty_chr_read_poll, |
886 | 6f97dba0 | aliguori | pty_chr_read, NULL, chr);
|
887 | 6f97dba0 | aliguori | s->polling = 1;
|
888 | 6f97dba0 | aliguori | /*
|
889 | 6f97dba0 | aliguori | * Short timeout here: just need wait long enougth that qemu makes
|
890 | 6f97dba0 | aliguori | * it through the poll loop once. When reconnected we want a
|
891 | 6f97dba0 | aliguori | * short timeout so we notice it almost instantly. Otherwise
|
892 | 6f97dba0 | aliguori | * read() gives us -EIO instantly, making pty_chr_state() reset the
|
893 | 6f97dba0 | aliguori | * timeout to the normal (much longer) poll interval before the
|
894 | 6f97dba0 | aliguori | * timer triggers.
|
895 | 6f97dba0 | aliguori | */
|
896 | 6f97dba0 | aliguori | qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
|
897 | 6f97dba0 | aliguori | } |
898 | 6f97dba0 | aliguori | |
899 | 6f97dba0 | aliguori | static void pty_chr_state(CharDriverState *chr, int connected) |
900 | 6f97dba0 | aliguori | { |
901 | 6f97dba0 | aliguori | PtyCharDriver *s = chr->opaque; |
902 | 6f97dba0 | aliguori | |
903 | 6f97dba0 | aliguori | if (!connected) {
|
904 | 6f97dba0 | aliguori | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
905 | 6f97dba0 | aliguori | s->connected = 0;
|
906 | 6f97dba0 | aliguori | s->polling = 0;
|
907 | 6f97dba0 | aliguori | /* (re-)connect poll interval for idle guests: once per second.
|
908 | 6f97dba0 | aliguori | * We check more frequently in case the guests sends data to
|
909 | 6f97dba0 | aliguori | * the virtual device linked to our pty. */
|
910 | 6f97dba0 | aliguori | qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
|
911 | 6f97dba0 | aliguori | } else {
|
912 | 6f97dba0 | aliguori | if (!s->connected)
|
913 | 127338e6 | Amit Shah | qemu_chr_generic_open(chr); |
914 | 6f97dba0 | aliguori | s->connected = 1;
|
915 | 6f97dba0 | aliguori | } |
916 | 6f97dba0 | aliguori | } |
917 | 6f97dba0 | aliguori | |
918 | 6f97dba0 | aliguori | static void pty_chr_timer(void *opaque) |
919 | 6f97dba0 | aliguori | { |
920 | 6f97dba0 | aliguori | struct CharDriverState *chr = opaque;
|
921 | 6f97dba0 | aliguori | PtyCharDriver *s = chr->opaque; |
922 | 6f97dba0 | aliguori | |
923 | 6f97dba0 | aliguori | if (s->connected)
|
924 | 6f97dba0 | aliguori | return;
|
925 | 6f97dba0 | aliguori | if (s->polling) {
|
926 | 6f97dba0 | aliguori | /* If we arrive here without polling being cleared due
|
927 | 6f97dba0 | aliguori | * read returning -EIO, then we are (re-)connected */
|
928 | 6f97dba0 | aliguori | pty_chr_state(chr, 1);
|
929 | 6f97dba0 | aliguori | return;
|
930 | 6f97dba0 | aliguori | } |
931 | 6f97dba0 | aliguori | |
932 | 6f97dba0 | aliguori | /* Next poll ... */
|
933 | 6f97dba0 | aliguori | pty_chr_update_read_handler(chr); |
934 | 6f97dba0 | aliguori | } |
935 | 6f97dba0 | aliguori | |
936 | 6f97dba0 | aliguori | static void pty_chr_close(struct CharDriverState *chr) |
937 | 6f97dba0 | aliguori | { |
938 | 6f97dba0 | aliguori | PtyCharDriver *s = chr->opaque; |
939 | 6f97dba0 | aliguori | |
940 | 6f97dba0 | aliguori | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
941 | 6f97dba0 | aliguori | close(s->fd); |
942 | 819f56b7 | aliguori | qemu_del_timer(s->timer); |
943 | 819f56b7 | aliguori | qemu_free_timer(s->timer); |
944 | 6f97dba0 | aliguori | qemu_free(s); |
945 | 793cbfb5 | Amit Shah | qemu_chr_event(chr, CHR_EVENT_CLOSED); |
946 | 6f97dba0 | aliguori | } |
947 | 6f97dba0 | aliguori | |
948 | 4490dadf | Gerd Hoffmann | static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
|
949 | 6f97dba0 | aliguori | { |
950 | 6f97dba0 | aliguori | CharDriverState *chr; |
951 | 6f97dba0 | aliguori | PtyCharDriver *s; |
952 | 6f97dba0 | aliguori | struct termios tty;
|
953 | 6f97dba0 | aliguori | int slave_fd, len;
|
954 | c5e97233 | blueswir1 | #if defined(__OpenBSD__) || defined(__DragonFly__)
|
955 | 6f97dba0 | aliguori | char pty_name[PATH_MAX];
|
956 | 6f97dba0 | aliguori | #define q_ptsname(x) pty_name
|
957 | 6f97dba0 | aliguori | #else
|
958 | 6f97dba0 | aliguori | char *pty_name = NULL; |
959 | 6f97dba0 | aliguori | #define q_ptsname(x) ptsname(x)
|
960 | 6f97dba0 | aliguori | #endif
|
961 | 6f97dba0 | aliguori | |
962 | 6f97dba0 | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
963 | 6f97dba0 | aliguori | s = qemu_mallocz(sizeof(PtyCharDriver));
|
964 | 6f97dba0 | aliguori | |
965 | 6f97dba0 | aliguori | if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) { |
966 | 6f97dba0 | aliguori | return NULL; |
967 | 6f97dba0 | aliguori | } |
968 | 6f97dba0 | aliguori | |
969 | 6f97dba0 | aliguori | /* Set raw attributes on the pty. */
|
970 | c3b972c3 | aliguori | tcgetattr(slave_fd, &tty); |
971 | 6f97dba0 | aliguori | cfmakeraw(&tty); |
972 | 6f97dba0 | aliguori | tcsetattr(slave_fd, TCSAFLUSH, &tty); |
973 | 6f97dba0 | aliguori | close(slave_fd); |
974 | 6f97dba0 | aliguori | |
975 | 6f97dba0 | aliguori | len = strlen(q_ptsname(s->fd)) + 5;
|
976 | 6f97dba0 | aliguori | chr->filename = qemu_malloc(len); |
977 | 6f97dba0 | aliguori | snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
|
978 | 4490dadf | Gerd Hoffmann | qemu_opt_set(opts, "path", q_ptsname(s->fd));
|
979 | 6f97dba0 | aliguori | fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
|
980 | 6f97dba0 | aliguori | |
981 | 6f97dba0 | aliguori | chr->opaque = s; |
982 | 6f97dba0 | aliguori | chr->chr_write = pty_chr_write; |
983 | 6f97dba0 | aliguori | chr->chr_update_read_handler = pty_chr_update_read_handler; |
984 | 6f97dba0 | aliguori | chr->chr_close = pty_chr_close; |
985 | 6f97dba0 | aliguori | |
986 | 6f97dba0 | aliguori | s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr); |
987 | 6f97dba0 | aliguori | |
988 | 6f97dba0 | aliguori | return chr;
|
989 | 6f97dba0 | aliguori | } |
990 | 6f97dba0 | aliguori | |
991 | 6f97dba0 | aliguori | static void tty_serial_init(int fd, int speed, |
992 | 6f97dba0 | aliguori | int parity, int data_bits, int stop_bits) |
993 | 6f97dba0 | aliguori | { |
994 | 6f97dba0 | aliguori | struct termios tty;
|
995 | 6f97dba0 | aliguori | speed_t spd; |
996 | 6f97dba0 | aliguori | |
997 | 6f97dba0 | aliguori | #if 0
|
998 | 6f97dba0 | aliguori | printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
|
999 | 6f97dba0 | aliguori | speed, parity, data_bits, stop_bits);
|
1000 | 6f97dba0 | aliguori | #endif
|
1001 | 6f97dba0 | aliguori | tcgetattr (fd, &tty); |
1002 | 6f97dba0 | aliguori | |
1003 | 45eea13b | Stefan Weil | #define check_speed(val) if (speed <= val) { spd = B##val; break; } |
1004 | 45eea13b | Stefan Weil | speed = speed * 10 / 11; |
1005 | 45eea13b | Stefan Weil | do {
|
1006 | 45eea13b | Stefan Weil | check_speed(50);
|
1007 | 45eea13b | Stefan Weil | check_speed(75);
|
1008 | 45eea13b | Stefan Weil | check_speed(110);
|
1009 | 45eea13b | Stefan Weil | check_speed(134);
|
1010 | 45eea13b | Stefan Weil | check_speed(150);
|
1011 | 45eea13b | Stefan Weil | check_speed(200);
|
1012 | 45eea13b | Stefan Weil | check_speed(300);
|
1013 | 45eea13b | Stefan Weil | check_speed(600);
|
1014 | 45eea13b | Stefan Weil | check_speed(1200);
|
1015 | 45eea13b | Stefan Weil | check_speed(1800);
|
1016 | 45eea13b | Stefan Weil | check_speed(2400);
|
1017 | 45eea13b | Stefan Weil | check_speed(4800);
|
1018 | 45eea13b | Stefan Weil | check_speed(9600);
|
1019 | 45eea13b | Stefan Weil | check_speed(19200);
|
1020 | 45eea13b | Stefan Weil | check_speed(38400);
|
1021 | 45eea13b | Stefan Weil | /* Non-Posix values follow. They may be unsupported on some systems. */
|
1022 | 45eea13b | Stefan Weil | check_speed(57600);
|
1023 | 45eea13b | Stefan Weil | check_speed(115200);
|
1024 | 45eea13b | Stefan Weil | #ifdef B230400
|
1025 | 45eea13b | Stefan Weil | check_speed(230400);
|
1026 | 45eea13b | Stefan Weil | #endif
|
1027 | 45eea13b | Stefan Weil | #ifdef B460800
|
1028 | 45eea13b | Stefan Weil | check_speed(460800);
|
1029 | 45eea13b | Stefan Weil | #endif
|
1030 | 45eea13b | Stefan Weil | #ifdef B500000
|
1031 | 45eea13b | Stefan Weil | check_speed(500000);
|
1032 | 45eea13b | Stefan Weil | #endif
|
1033 | 45eea13b | Stefan Weil | #ifdef B576000
|
1034 | 45eea13b | Stefan Weil | check_speed(576000);
|
1035 | 45eea13b | Stefan Weil | #endif
|
1036 | 45eea13b | Stefan Weil | #ifdef B921600
|
1037 | 45eea13b | Stefan Weil | check_speed(921600);
|
1038 | 45eea13b | Stefan Weil | #endif
|
1039 | 45eea13b | Stefan Weil | #ifdef B1000000
|
1040 | 45eea13b | Stefan Weil | check_speed(1000000);
|
1041 | 45eea13b | Stefan Weil | #endif
|
1042 | 45eea13b | Stefan Weil | #ifdef B1152000
|
1043 | 45eea13b | Stefan Weil | check_speed(1152000);
|
1044 | 45eea13b | Stefan Weil | #endif
|
1045 | 45eea13b | Stefan Weil | #ifdef B1500000
|
1046 | 45eea13b | Stefan Weil | check_speed(1500000);
|
1047 | 45eea13b | Stefan Weil | #endif
|
1048 | 45eea13b | Stefan Weil | #ifdef B2000000
|
1049 | 45eea13b | Stefan Weil | check_speed(2000000);
|
1050 | 45eea13b | Stefan Weil | #endif
|
1051 | 45eea13b | Stefan Weil | #ifdef B2500000
|
1052 | 45eea13b | Stefan Weil | check_speed(2500000);
|
1053 | 45eea13b | Stefan Weil | #endif
|
1054 | 45eea13b | Stefan Weil | #ifdef B3000000
|
1055 | 45eea13b | Stefan Weil | check_speed(3000000);
|
1056 | 45eea13b | Stefan Weil | #endif
|
1057 | 45eea13b | Stefan Weil | #ifdef B3500000
|
1058 | 45eea13b | Stefan Weil | check_speed(3500000);
|
1059 | 45eea13b | Stefan Weil | #endif
|
1060 | 45eea13b | Stefan Weil | #ifdef B4000000
|
1061 | 45eea13b | Stefan Weil | check_speed(4000000);
|
1062 | 45eea13b | Stefan Weil | #endif
|
1063 | 6f97dba0 | aliguori | spd = B115200; |
1064 | 45eea13b | Stefan Weil | } while (0); |
1065 | 6f97dba0 | aliguori | |
1066 | 6f97dba0 | aliguori | cfsetispeed(&tty, spd); |
1067 | 6f97dba0 | aliguori | cfsetospeed(&tty, spd); |
1068 | 6f97dba0 | aliguori | |
1069 | 6f97dba0 | aliguori | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
1070 | 6f97dba0 | aliguori | |INLCR|IGNCR|ICRNL|IXON); |
1071 | 6f97dba0 | aliguori | tty.c_oflag |= OPOST; |
1072 | 6f97dba0 | aliguori | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); |
1073 | 6f97dba0 | aliguori | tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); |
1074 | 6f97dba0 | aliguori | switch(data_bits) {
|
1075 | 6f97dba0 | aliguori | default:
|
1076 | 6f97dba0 | aliguori | case 8: |
1077 | 6f97dba0 | aliguori | tty.c_cflag |= CS8; |
1078 | 6f97dba0 | aliguori | break;
|
1079 | 6f97dba0 | aliguori | case 7: |
1080 | 6f97dba0 | aliguori | tty.c_cflag |= CS7; |
1081 | 6f97dba0 | aliguori | break;
|
1082 | 6f97dba0 | aliguori | case 6: |
1083 | 6f97dba0 | aliguori | tty.c_cflag |= CS6; |
1084 | 6f97dba0 | aliguori | break;
|
1085 | 6f97dba0 | aliguori | case 5: |
1086 | 6f97dba0 | aliguori | tty.c_cflag |= CS5; |
1087 | 6f97dba0 | aliguori | break;
|
1088 | 6f97dba0 | aliguori | } |
1089 | 6f97dba0 | aliguori | switch(parity) {
|
1090 | 6f97dba0 | aliguori | default:
|
1091 | 6f97dba0 | aliguori | case 'N': |
1092 | 6f97dba0 | aliguori | break;
|
1093 | 6f97dba0 | aliguori | case 'E': |
1094 | 6f97dba0 | aliguori | tty.c_cflag |= PARENB; |
1095 | 6f97dba0 | aliguori | break;
|
1096 | 6f97dba0 | aliguori | case 'O': |
1097 | 6f97dba0 | aliguori | tty.c_cflag |= PARENB | PARODD; |
1098 | 6f97dba0 | aliguori | break;
|
1099 | 6f97dba0 | aliguori | } |
1100 | 6f97dba0 | aliguori | if (stop_bits == 2) |
1101 | 6f97dba0 | aliguori | tty.c_cflag |= CSTOPB; |
1102 | 6f97dba0 | aliguori | |
1103 | 6f97dba0 | aliguori | tcsetattr (fd, TCSANOW, &tty); |
1104 | 6f97dba0 | aliguori | } |
1105 | 6f97dba0 | aliguori | |
1106 | 6f97dba0 | aliguori | static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) |
1107 | 6f97dba0 | aliguori | { |
1108 | 6f97dba0 | aliguori | FDCharDriver *s = chr->opaque; |
1109 | 6f97dba0 | aliguori | |
1110 | 6f97dba0 | aliguori | switch(cmd) {
|
1111 | 6f97dba0 | aliguori | case CHR_IOCTL_SERIAL_SET_PARAMS:
|
1112 | 6f97dba0 | aliguori | { |
1113 | 6f97dba0 | aliguori | QEMUSerialSetParams *ssp = arg; |
1114 | 6f97dba0 | aliguori | tty_serial_init(s->fd_in, ssp->speed, ssp->parity, |
1115 | 6f97dba0 | aliguori | ssp->data_bits, ssp->stop_bits); |
1116 | 6f97dba0 | aliguori | } |
1117 | 6f97dba0 | aliguori | break;
|
1118 | 6f97dba0 | aliguori | case CHR_IOCTL_SERIAL_SET_BREAK:
|
1119 | 6f97dba0 | aliguori | { |
1120 | 6f97dba0 | aliguori | int enable = *(int *)arg; |
1121 | 6f97dba0 | aliguori | if (enable)
|
1122 | 6f97dba0 | aliguori | tcsendbreak(s->fd_in, 1);
|
1123 | 6f97dba0 | aliguori | } |
1124 | 6f97dba0 | aliguori | break;
|
1125 | 6f97dba0 | aliguori | case CHR_IOCTL_SERIAL_GET_TIOCM:
|
1126 | 6f97dba0 | aliguori | { |
1127 | 6f97dba0 | aliguori | int sarg = 0; |
1128 | 6f97dba0 | aliguori | int *targ = (int *)arg; |
1129 | 6f97dba0 | aliguori | ioctl(s->fd_in, TIOCMGET, &sarg); |
1130 | 6f97dba0 | aliguori | *targ = 0;
|
1131 | b4abdfa4 | aurel32 | if (sarg & TIOCM_CTS)
|
1132 | 6f97dba0 | aliguori | *targ |= CHR_TIOCM_CTS; |
1133 | b4abdfa4 | aurel32 | if (sarg & TIOCM_CAR)
|
1134 | 6f97dba0 | aliguori | *targ |= CHR_TIOCM_CAR; |
1135 | b4abdfa4 | aurel32 | if (sarg & TIOCM_DSR)
|
1136 | 6f97dba0 | aliguori | *targ |= CHR_TIOCM_DSR; |
1137 | b4abdfa4 | aurel32 | if (sarg & TIOCM_RI)
|
1138 | 6f97dba0 | aliguori | *targ |= CHR_TIOCM_RI; |
1139 | b4abdfa4 | aurel32 | if (sarg & TIOCM_DTR)
|
1140 | 6f97dba0 | aliguori | *targ |= CHR_TIOCM_DTR; |
1141 | b4abdfa4 | aurel32 | if (sarg & TIOCM_RTS)
|
1142 | 6f97dba0 | aliguori | *targ |= CHR_TIOCM_RTS; |
1143 | 6f97dba0 | aliguori | } |
1144 | 6f97dba0 | aliguori | break;
|
1145 | 6f97dba0 | aliguori | case CHR_IOCTL_SERIAL_SET_TIOCM:
|
1146 | 6f97dba0 | aliguori | { |
1147 | 6f97dba0 | aliguori | int sarg = *(int *)arg; |
1148 | 6f97dba0 | aliguori | int targ = 0; |
1149 | b4abdfa4 | aurel32 | ioctl(s->fd_in, TIOCMGET, &targ); |
1150 | b4abdfa4 | aurel32 | targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR |
1151 | b4abdfa4 | aurel32 | | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS); |
1152 | b4abdfa4 | aurel32 | if (sarg & CHR_TIOCM_CTS)
|
1153 | b4abdfa4 | aurel32 | targ |= TIOCM_CTS; |
1154 | b4abdfa4 | aurel32 | if (sarg & CHR_TIOCM_CAR)
|
1155 | b4abdfa4 | aurel32 | targ |= TIOCM_CAR; |
1156 | b4abdfa4 | aurel32 | if (sarg & CHR_TIOCM_DSR)
|
1157 | b4abdfa4 | aurel32 | targ |= TIOCM_DSR; |
1158 | b4abdfa4 | aurel32 | if (sarg & CHR_TIOCM_RI)
|
1159 | b4abdfa4 | aurel32 | targ |= TIOCM_RI; |
1160 | b4abdfa4 | aurel32 | if (sarg & CHR_TIOCM_DTR)
|
1161 | 6f97dba0 | aliguori | targ |= TIOCM_DTR; |
1162 | b4abdfa4 | aurel32 | if (sarg & CHR_TIOCM_RTS)
|
1163 | 6f97dba0 | aliguori | targ |= TIOCM_RTS; |
1164 | 6f97dba0 | aliguori | ioctl(s->fd_in, TIOCMSET, &targ); |
1165 | 6f97dba0 | aliguori | } |
1166 | 6f97dba0 | aliguori | break;
|
1167 | 6f97dba0 | aliguori | default:
|
1168 | 6f97dba0 | aliguori | return -ENOTSUP;
|
1169 | 6f97dba0 | aliguori | } |
1170 | 6f97dba0 | aliguori | return 0; |
1171 | 6f97dba0 | aliguori | } |
1172 | 6f97dba0 | aliguori | |
1173 | 48b76496 | Gerd Hoffmann | static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
|
1174 | 6f97dba0 | aliguori | { |
1175 | 48b76496 | Gerd Hoffmann | const char *filename = qemu_opt_get(opts, "path"); |
1176 | 6f97dba0 | aliguori | CharDriverState *chr; |
1177 | 6f97dba0 | aliguori | int fd;
|
1178 | 6f97dba0 | aliguori | |
1179 | 6f97dba0 | aliguori | TFR(fd = open(filename, O_RDWR | O_NONBLOCK)); |
1180 | 6f97dba0 | aliguori | tty_serial_init(fd, 115200, 'N', 8, 1); |
1181 | 6f97dba0 | aliguori | chr = qemu_chr_open_fd(fd, fd); |
1182 | 6f97dba0 | aliguori | if (!chr) {
|
1183 | 6f97dba0 | aliguori | close(fd); |
1184 | 6f97dba0 | aliguori | return NULL; |
1185 | 6f97dba0 | aliguori | } |
1186 | 6f97dba0 | aliguori | chr->chr_ioctl = tty_serial_ioctl; |
1187 | 127338e6 | Amit Shah | qemu_chr_generic_open(chr); |
1188 | 6f97dba0 | aliguori | return chr;
|
1189 | 6f97dba0 | aliguori | } |
1190 | 6f97dba0 | aliguori | #else /* ! __linux__ && ! __sun__ */ |
1191 | 6f97dba0 | aliguori | static CharDriverState *qemu_chr_open_pty(void) |
1192 | 6f97dba0 | aliguori | { |
1193 | 6f97dba0 | aliguori | return NULL; |
1194 | 6f97dba0 | aliguori | } |
1195 | 6f97dba0 | aliguori | #endif /* __linux__ || __sun__ */ |
1196 | 6f97dba0 | aliguori | |
1197 | 6f97dba0 | aliguori | #if defined(__linux__)
|
1198 | 6f97dba0 | aliguori | typedef struct { |
1199 | 6f97dba0 | aliguori | int fd;
|
1200 | 6f97dba0 | aliguori | int mode;
|
1201 | 6f97dba0 | aliguori | } ParallelCharDriver; |
1202 | 6f97dba0 | aliguori | |
1203 | 6f97dba0 | aliguori | static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode) |
1204 | 6f97dba0 | aliguori | { |
1205 | 6f97dba0 | aliguori | if (s->mode != mode) {
|
1206 | 6f97dba0 | aliguori | int m = mode;
|
1207 | 6f97dba0 | aliguori | if (ioctl(s->fd, PPSETMODE, &m) < 0) |
1208 | 6f97dba0 | aliguori | return 0; |
1209 | 6f97dba0 | aliguori | s->mode = mode; |
1210 | 6f97dba0 | aliguori | } |
1211 | 6f97dba0 | aliguori | return 1; |
1212 | 6f97dba0 | aliguori | } |
1213 | 6f97dba0 | aliguori | |
1214 | 6f97dba0 | aliguori | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
1215 | 6f97dba0 | aliguori | { |
1216 | 6f97dba0 | aliguori | ParallelCharDriver *drv = chr->opaque; |
1217 | 6f97dba0 | aliguori | int fd = drv->fd;
|
1218 | 6f97dba0 | aliguori | uint8_t b; |
1219 | 6f97dba0 | aliguori | |
1220 | 6f97dba0 | aliguori | switch(cmd) {
|
1221 | 6f97dba0 | aliguori | case CHR_IOCTL_PP_READ_DATA:
|
1222 | 6f97dba0 | aliguori | if (ioctl(fd, PPRDATA, &b) < 0) |
1223 | 6f97dba0 | aliguori | return -ENOTSUP;
|
1224 | 6f97dba0 | aliguori | *(uint8_t *)arg = b; |
1225 | 6f97dba0 | aliguori | break;
|
1226 | 6f97dba0 | aliguori | case CHR_IOCTL_PP_WRITE_DATA:
|
1227 | 6f97dba0 | aliguori | b = *(uint8_t *)arg; |
1228 | 6f97dba0 | aliguori | if (ioctl(fd, PPWDATA, &b) < 0) |
1229 | 6f97dba0 | aliguori | return -ENOTSUP;
|
1230 | 6f97dba0 | aliguori | break;
|
1231 | 6f97dba0 | aliguori | case CHR_IOCTL_PP_READ_CONTROL:
|
1232 | 6f97dba0 | aliguori | if (ioctl(fd, PPRCONTROL, &b) < 0) |
1233 | 6f97dba0 | aliguori | return -ENOTSUP;
|
1234 | 6f97dba0 | aliguori | /* Linux gives only the lowest bits, and no way to know data
|
1235 | 6f97dba0 | aliguori | direction! For better compatibility set the fixed upper
|
1236 | 6f97dba0 | aliguori | bits. */
|
1237 | 6f97dba0 | aliguori | *(uint8_t *)arg = b | 0xc0;
|
1238 | 6f97dba0 | aliguori | break;
|
1239 | 6f97dba0 | aliguori | case CHR_IOCTL_PP_WRITE_CONTROL:
|
1240 | 6f97dba0 | aliguori | b = *(uint8_t *)arg; |
1241 | 6f97dba0 | aliguori | if (ioctl(fd, PPWCONTROL, &b) < 0) |
1242 | 6f97dba0 | aliguori | return -ENOTSUP;
|
1243 | 6f97dba0 | aliguori | break;
|
1244 | 6f97dba0 | aliguori | case CHR_IOCTL_PP_READ_STATUS:
|
1245 | 6f97dba0 | aliguori | if (ioctl(fd, PPRSTATUS, &b) < 0) |
1246 | 6f97dba0 | aliguori | return -ENOTSUP;
|
1247 | 6f97dba0 | aliguori | *(uint8_t *)arg = b; |
1248 | 6f97dba0 | aliguori | break;
|
1249 | 6f97dba0 | aliguori | case CHR_IOCTL_PP_DATA_DIR:
|
1250 | 6f97dba0 | aliguori | if (ioctl(fd, PPDATADIR, (int *)arg) < 0) |
1251 | 6f97dba0 | aliguori | return -ENOTSUP;
|
1252 | 6f97dba0 | aliguori | break;
|
1253 | 6f97dba0 | aliguori | case CHR_IOCTL_PP_EPP_READ_ADDR:
|
1254 | 6f97dba0 | aliguori | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
|
1255 | 6f97dba0 | aliguori | struct ParallelIOArg *parg = arg;
|
1256 | 6f97dba0 | aliguori | int n = read(fd, parg->buffer, parg->count);
|
1257 | 6f97dba0 | aliguori | if (n != parg->count) {
|
1258 | 6f97dba0 | aliguori | return -EIO;
|
1259 | 6f97dba0 | aliguori | } |
1260 | 6f97dba0 | aliguori | } |
1261 | 6f97dba0 | aliguori | break;
|
1262 | 6f97dba0 | aliguori | case CHR_IOCTL_PP_EPP_READ:
|
1263 | 6f97dba0 | aliguori | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
|
1264 | 6f97dba0 | aliguori | struct ParallelIOArg *parg = arg;
|
1265 | 6f97dba0 | aliguori | int n = read(fd, parg->buffer, parg->count);
|
1266 | 6f97dba0 | aliguori | if (n != parg->count) {
|
1267 | 6f97dba0 | aliguori | return -EIO;
|
1268 | 6f97dba0 | aliguori | } |
1269 | 6f97dba0 | aliguori | } |
1270 | 6f97dba0 | aliguori | break;
|
1271 | 6f97dba0 | aliguori | case CHR_IOCTL_PP_EPP_WRITE_ADDR:
|
1272 | 6f97dba0 | aliguori | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
|
1273 | 6f97dba0 | aliguori | struct ParallelIOArg *parg = arg;
|
1274 | 6f97dba0 | aliguori | int n = write(fd, parg->buffer, parg->count);
|
1275 | 6f97dba0 | aliguori | if (n != parg->count) {
|
1276 | 6f97dba0 | aliguori | return -EIO;
|
1277 | 6f97dba0 | aliguori | } |
1278 | 6f97dba0 | aliguori | } |
1279 | 6f97dba0 | aliguori | break;
|
1280 | 6f97dba0 | aliguori | case CHR_IOCTL_PP_EPP_WRITE:
|
1281 | 6f97dba0 | aliguori | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
|
1282 | 6f97dba0 | aliguori | struct ParallelIOArg *parg = arg;
|
1283 | 6f97dba0 | aliguori | int n = write(fd, parg->buffer, parg->count);
|
1284 | 6f97dba0 | aliguori | if (n != parg->count) {
|
1285 | 6f97dba0 | aliguori | return -EIO;
|
1286 | 6f97dba0 | aliguori | } |
1287 | 6f97dba0 | aliguori | } |
1288 | 6f97dba0 | aliguori | break;
|
1289 | 6f97dba0 | aliguori | default:
|
1290 | 6f97dba0 | aliguori | return -ENOTSUP;
|
1291 | 6f97dba0 | aliguori | } |
1292 | 6f97dba0 | aliguori | return 0; |
1293 | 6f97dba0 | aliguori | } |
1294 | 6f97dba0 | aliguori | |
1295 | 6f97dba0 | aliguori | static void pp_close(CharDriverState *chr) |
1296 | 6f97dba0 | aliguori | { |
1297 | 6f97dba0 | aliguori | ParallelCharDriver *drv = chr->opaque; |
1298 | 6f97dba0 | aliguori | int fd = drv->fd;
|
1299 | 6f97dba0 | aliguori | |
1300 | 6f97dba0 | aliguori | pp_hw_mode(drv, IEEE1284_MODE_COMPAT); |
1301 | 6f97dba0 | aliguori | ioctl(fd, PPRELEASE); |
1302 | 6f97dba0 | aliguori | close(fd); |
1303 | 6f97dba0 | aliguori | qemu_free(drv); |
1304 | 793cbfb5 | Amit Shah | qemu_chr_event(chr, CHR_EVENT_CLOSED); |
1305 | 6f97dba0 | aliguori | } |
1306 | 6f97dba0 | aliguori | |
1307 | 48b76496 | Gerd Hoffmann | static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
|
1308 | 6f97dba0 | aliguori | { |
1309 | 48b76496 | Gerd Hoffmann | const char *filename = qemu_opt_get(opts, "path"); |
1310 | 6f97dba0 | aliguori | CharDriverState *chr; |
1311 | 6f97dba0 | aliguori | ParallelCharDriver *drv; |
1312 | 6f97dba0 | aliguori | int fd;
|
1313 | 6f97dba0 | aliguori | |
1314 | 6f97dba0 | aliguori | TFR(fd = open(filename, O_RDWR)); |
1315 | 6f97dba0 | aliguori | if (fd < 0) |
1316 | 6f97dba0 | aliguori | return NULL; |
1317 | 6f97dba0 | aliguori | |
1318 | 6f97dba0 | aliguori | if (ioctl(fd, PPCLAIM) < 0) { |
1319 | 6f97dba0 | aliguori | close(fd); |
1320 | 6f97dba0 | aliguori | return NULL; |
1321 | 6f97dba0 | aliguori | } |
1322 | 6f97dba0 | aliguori | |
1323 | 6f97dba0 | aliguori | drv = qemu_mallocz(sizeof(ParallelCharDriver));
|
1324 | 6f97dba0 | aliguori | drv->fd = fd; |
1325 | 6f97dba0 | aliguori | drv->mode = IEEE1284_MODE_COMPAT; |
1326 | 6f97dba0 | aliguori | |
1327 | 6f97dba0 | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
1328 | 6f97dba0 | aliguori | chr->chr_write = null_chr_write; |
1329 | 6f97dba0 | aliguori | chr->chr_ioctl = pp_ioctl; |
1330 | 6f97dba0 | aliguori | chr->chr_close = pp_close; |
1331 | 6f97dba0 | aliguori | chr->opaque = drv; |
1332 | 6f97dba0 | aliguori | |
1333 | 127338e6 | Amit Shah | qemu_chr_generic_open(chr); |
1334 | 6f97dba0 | aliguori | |
1335 | 6f97dba0 | aliguori | return chr;
|
1336 | 6f97dba0 | aliguori | } |
1337 | 6f97dba0 | aliguori | #endif /* __linux__ */ |
1338 | 6f97dba0 | aliguori | |
1339 | c5e97233 | blueswir1 | #if defined(__FreeBSD__) || defined(__DragonFly__)
|
1340 | 6972f935 | blueswir1 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
1341 | 6972f935 | blueswir1 | { |
1342 | 6972f935 | blueswir1 | int fd = (int)chr->opaque; |
1343 | 6972f935 | blueswir1 | uint8_t b; |
1344 | 6972f935 | blueswir1 | |
1345 | 6972f935 | blueswir1 | switch(cmd) {
|
1346 | 6972f935 | blueswir1 | case CHR_IOCTL_PP_READ_DATA:
|
1347 | 6972f935 | blueswir1 | if (ioctl(fd, PPIGDATA, &b) < 0) |
1348 | 6972f935 | blueswir1 | return -ENOTSUP;
|
1349 | 6972f935 | blueswir1 | *(uint8_t *)arg = b; |
1350 | 6972f935 | blueswir1 | break;
|
1351 | 6972f935 | blueswir1 | case CHR_IOCTL_PP_WRITE_DATA:
|
1352 | 6972f935 | blueswir1 | b = *(uint8_t *)arg; |
1353 | 6972f935 | blueswir1 | if (ioctl(fd, PPISDATA, &b) < 0) |
1354 | 6972f935 | blueswir1 | return -ENOTSUP;
|
1355 | 6972f935 | blueswir1 | break;
|
1356 | 6972f935 | blueswir1 | case CHR_IOCTL_PP_READ_CONTROL:
|
1357 | 6972f935 | blueswir1 | if (ioctl(fd, PPIGCTRL, &b) < 0) |
1358 | 6972f935 | blueswir1 | return -ENOTSUP;
|
1359 | 6972f935 | blueswir1 | *(uint8_t *)arg = b; |
1360 | 6972f935 | blueswir1 | break;
|
1361 | 6972f935 | blueswir1 | case CHR_IOCTL_PP_WRITE_CONTROL:
|
1362 | 6972f935 | blueswir1 | b = *(uint8_t *)arg; |
1363 | 6972f935 | blueswir1 | if (ioctl(fd, PPISCTRL, &b) < 0) |
1364 | 6972f935 | blueswir1 | return -ENOTSUP;
|
1365 | 6972f935 | blueswir1 | break;
|
1366 | 6972f935 | blueswir1 | case CHR_IOCTL_PP_READ_STATUS:
|
1367 | 6972f935 | blueswir1 | if (ioctl(fd, PPIGSTATUS, &b) < 0) |
1368 | 6972f935 | blueswir1 | return -ENOTSUP;
|
1369 | 6972f935 | blueswir1 | *(uint8_t *)arg = b; |
1370 | 6972f935 | blueswir1 | break;
|
1371 | 6972f935 | blueswir1 | default:
|
1372 | 6972f935 | blueswir1 | return -ENOTSUP;
|
1373 | 6972f935 | blueswir1 | } |
1374 | 6972f935 | blueswir1 | return 0; |
1375 | 6972f935 | blueswir1 | } |
1376 | 6972f935 | blueswir1 | |
1377 | 48b76496 | Gerd Hoffmann | static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
|
1378 | 6972f935 | blueswir1 | { |
1379 | 48b76496 | Gerd Hoffmann | const char *filename = qemu_opt_get(opts, "path"); |
1380 | 6972f935 | blueswir1 | CharDriverState *chr; |
1381 | 6972f935 | blueswir1 | int fd;
|
1382 | 6972f935 | blueswir1 | |
1383 | 6972f935 | blueswir1 | fd = open(filename, O_RDWR); |
1384 | 6972f935 | blueswir1 | if (fd < 0) |
1385 | 6972f935 | blueswir1 | return NULL; |
1386 | 6972f935 | blueswir1 | |
1387 | 6972f935 | blueswir1 | chr = qemu_mallocz(sizeof(CharDriverState));
|
1388 | 6972f935 | blueswir1 | chr->opaque = (void *)fd;
|
1389 | 6972f935 | blueswir1 | chr->chr_write = null_chr_write; |
1390 | 6972f935 | blueswir1 | chr->chr_ioctl = pp_ioctl; |
1391 | 6972f935 | blueswir1 | return chr;
|
1392 | 6972f935 | blueswir1 | } |
1393 | 6972f935 | blueswir1 | #endif
|
1394 | 6972f935 | blueswir1 | |
1395 | 6f97dba0 | aliguori | #else /* _WIN32 */ |
1396 | 6f97dba0 | aliguori | |
1397 | 6f97dba0 | aliguori | typedef struct { |
1398 | 6f97dba0 | aliguori | int max_size;
|
1399 | 6f97dba0 | aliguori | HANDLE hcom, hrecv, hsend; |
1400 | 6f97dba0 | aliguori | OVERLAPPED orecv, osend; |
1401 | 6f97dba0 | aliguori | BOOL fpipe; |
1402 | 6f97dba0 | aliguori | DWORD len; |
1403 | 6f97dba0 | aliguori | } WinCharState; |
1404 | 6f97dba0 | aliguori | |
1405 | 6f97dba0 | aliguori | #define NSENDBUF 2048 |
1406 | 6f97dba0 | aliguori | #define NRECVBUF 2048 |
1407 | 6f97dba0 | aliguori | #define MAXCONNECT 1 |
1408 | 6f97dba0 | aliguori | #define NTIMEOUT 5000 |
1409 | 6f97dba0 | aliguori | |
1410 | 6f97dba0 | aliguori | static int win_chr_poll(void *opaque); |
1411 | 6f97dba0 | aliguori | static int win_chr_pipe_poll(void *opaque); |
1412 | 6f97dba0 | aliguori | |
1413 | 6f97dba0 | aliguori | static void win_chr_close(CharDriverState *chr) |
1414 | 6f97dba0 | aliguori | { |
1415 | 6f97dba0 | aliguori | WinCharState *s = chr->opaque; |
1416 | 6f97dba0 | aliguori | |
1417 | 6f97dba0 | aliguori | if (s->hsend) {
|
1418 | 6f97dba0 | aliguori | CloseHandle(s->hsend); |
1419 | 6f97dba0 | aliguori | s->hsend = NULL;
|
1420 | 6f97dba0 | aliguori | } |
1421 | 6f97dba0 | aliguori | if (s->hrecv) {
|
1422 | 6f97dba0 | aliguori | CloseHandle(s->hrecv); |
1423 | 6f97dba0 | aliguori | s->hrecv = NULL;
|
1424 | 6f97dba0 | aliguori | } |
1425 | 6f97dba0 | aliguori | if (s->hcom) {
|
1426 | 6f97dba0 | aliguori | CloseHandle(s->hcom); |
1427 | 6f97dba0 | aliguori | s->hcom = NULL;
|
1428 | 6f97dba0 | aliguori | } |
1429 | 6f97dba0 | aliguori | if (s->fpipe)
|
1430 | 6f97dba0 | aliguori | qemu_del_polling_cb(win_chr_pipe_poll, chr); |
1431 | 6f97dba0 | aliguori | else
|
1432 | 6f97dba0 | aliguori | qemu_del_polling_cb(win_chr_poll, chr); |
1433 | 793cbfb5 | Amit Shah | |
1434 | 793cbfb5 | Amit Shah | qemu_chr_event(chr, CHR_EVENT_CLOSED); |
1435 | 6f97dba0 | aliguori | } |
1436 | 6f97dba0 | aliguori | |
1437 | 6f97dba0 | aliguori | static int win_chr_init(CharDriverState *chr, const char *filename) |
1438 | 6f97dba0 | aliguori | { |
1439 | 6f97dba0 | aliguori | WinCharState *s = chr->opaque; |
1440 | 6f97dba0 | aliguori | COMMCONFIG comcfg; |
1441 | 6f97dba0 | aliguori | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; |
1442 | 6f97dba0 | aliguori | COMSTAT comstat; |
1443 | 6f97dba0 | aliguori | DWORD size; |
1444 | 6f97dba0 | aliguori | DWORD err; |
1445 | 6f97dba0 | aliguori | |
1446 | 6f97dba0 | aliguori | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
1447 | 6f97dba0 | aliguori | if (!s->hsend) {
|
1448 | 6f97dba0 | aliguori | fprintf(stderr, "Failed CreateEvent\n");
|
1449 | 6f97dba0 | aliguori | goto fail;
|
1450 | 6f97dba0 | aliguori | } |
1451 | 6f97dba0 | aliguori | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
1452 | 6f97dba0 | aliguori | if (!s->hrecv) {
|
1453 | 6f97dba0 | aliguori | fprintf(stderr, "Failed CreateEvent\n");
|
1454 | 6f97dba0 | aliguori | goto fail;
|
1455 | 6f97dba0 | aliguori | } |
1456 | 6f97dba0 | aliguori | |
1457 | 6f97dba0 | aliguori | s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, |
1458 | 6f97dba0 | aliguori | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
|
1459 | 6f97dba0 | aliguori | if (s->hcom == INVALID_HANDLE_VALUE) {
|
1460 | 6f97dba0 | aliguori | fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
|
1461 | 6f97dba0 | aliguori | s->hcom = NULL;
|
1462 | 6f97dba0 | aliguori | goto fail;
|
1463 | 6f97dba0 | aliguori | } |
1464 | 6f97dba0 | aliguori | |
1465 | 6f97dba0 | aliguori | if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
|
1466 | 6f97dba0 | aliguori | fprintf(stderr, "Failed SetupComm\n");
|
1467 | 6f97dba0 | aliguori | goto fail;
|
1468 | 6f97dba0 | aliguori | } |
1469 | 6f97dba0 | aliguori | |
1470 | 6f97dba0 | aliguori | ZeroMemory(&comcfg, sizeof(COMMCONFIG));
|
1471 | 6f97dba0 | aliguori | size = sizeof(COMMCONFIG);
|
1472 | 6f97dba0 | aliguori | GetDefaultCommConfig(filename, &comcfg, &size); |
1473 | 6f97dba0 | aliguori | comcfg.dcb.DCBlength = sizeof(DCB);
|
1474 | 6f97dba0 | aliguori | CommConfigDialog(filename, NULL, &comcfg);
|
1475 | 6f97dba0 | aliguori | |
1476 | 6f97dba0 | aliguori | if (!SetCommState(s->hcom, &comcfg.dcb)) {
|
1477 | 6f97dba0 | aliguori | fprintf(stderr, "Failed SetCommState\n");
|
1478 | 6f97dba0 | aliguori | goto fail;
|
1479 | 6f97dba0 | aliguori | } |
1480 | 6f97dba0 | aliguori | |
1481 | 6f97dba0 | aliguori | if (!SetCommMask(s->hcom, EV_ERR)) {
|
1482 | 6f97dba0 | aliguori | fprintf(stderr, "Failed SetCommMask\n");
|
1483 | 6f97dba0 | aliguori | goto fail;
|
1484 | 6f97dba0 | aliguori | } |
1485 | 6f97dba0 | aliguori | |
1486 | 6f97dba0 | aliguori | cto.ReadIntervalTimeout = MAXDWORD; |
1487 | 6f97dba0 | aliguori | if (!SetCommTimeouts(s->hcom, &cto)) {
|
1488 | 6f97dba0 | aliguori | fprintf(stderr, "Failed SetCommTimeouts\n");
|
1489 | 6f97dba0 | aliguori | goto fail;
|
1490 | 6f97dba0 | aliguori | } |
1491 | 6f97dba0 | aliguori | |
1492 | 6f97dba0 | aliguori | if (!ClearCommError(s->hcom, &err, &comstat)) {
|
1493 | 6f97dba0 | aliguori | fprintf(stderr, "Failed ClearCommError\n");
|
1494 | 6f97dba0 | aliguori | goto fail;
|
1495 | 6f97dba0 | aliguori | } |
1496 | 6f97dba0 | aliguori | qemu_add_polling_cb(win_chr_poll, chr); |
1497 | 6f97dba0 | aliguori | return 0; |
1498 | 6f97dba0 | aliguori | |
1499 | 6f97dba0 | aliguori | fail:
|
1500 | 6f97dba0 | aliguori | win_chr_close(chr); |
1501 | 6f97dba0 | aliguori | return -1; |
1502 | 6f97dba0 | aliguori | } |
1503 | 6f97dba0 | aliguori | |
1504 | 6f97dba0 | aliguori | static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) |
1505 | 6f97dba0 | aliguori | { |
1506 | 6f97dba0 | aliguori | WinCharState *s = chr->opaque; |
1507 | 6f97dba0 | aliguori | DWORD len, ret, size, err; |
1508 | 6f97dba0 | aliguori | |
1509 | 6f97dba0 | aliguori | len = len1; |
1510 | 6f97dba0 | aliguori | ZeroMemory(&s->osend, sizeof(s->osend));
|
1511 | 6f97dba0 | aliguori | s->osend.hEvent = s->hsend; |
1512 | 6f97dba0 | aliguori | while (len > 0) { |
1513 | 6f97dba0 | aliguori | if (s->hsend)
|
1514 | 6f97dba0 | aliguori | ret = WriteFile(s->hcom, buf, len, &size, &s->osend); |
1515 | 6f97dba0 | aliguori | else
|
1516 | 6f97dba0 | aliguori | ret = WriteFile(s->hcom, buf, len, &size, NULL);
|
1517 | 6f97dba0 | aliguori | if (!ret) {
|
1518 | 6f97dba0 | aliguori | err = GetLastError(); |
1519 | 6f97dba0 | aliguori | if (err == ERROR_IO_PENDING) {
|
1520 | 6f97dba0 | aliguori | ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); |
1521 | 6f97dba0 | aliguori | if (ret) {
|
1522 | 6f97dba0 | aliguori | buf += size; |
1523 | 6f97dba0 | aliguori | len -= size; |
1524 | 6f97dba0 | aliguori | } else {
|
1525 | 6f97dba0 | aliguori | break;
|
1526 | 6f97dba0 | aliguori | } |
1527 | 6f97dba0 | aliguori | } else {
|
1528 | 6f97dba0 | aliguori | break;
|
1529 | 6f97dba0 | aliguori | } |
1530 | 6f97dba0 | aliguori | } else {
|
1531 | 6f97dba0 | aliguori | buf += size; |
1532 | 6f97dba0 | aliguori | len -= size; |
1533 | 6f97dba0 | aliguori | } |
1534 | 6f97dba0 | aliguori | } |
1535 | 6f97dba0 | aliguori | return len1 - len;
|
1536 | 6f97dba0 | aliguori | } |
1537 | 6f97dba0 | aliguori | |
1538 | 6f97dba0 | aliguori | static int win_chr_read_poll(CharDriverState *chr) |
1539 | 6f97dba0 | aliguori | { |
1540 | 6f97dba0 | aliguori | WinCharState *s = chr->opaque; |
1541 | 6f97dba0 | aliguori | |
1542 | 6f97dba0 | aliguori | s->max_size = qemu_chr_can_read(chr); |
1543 | 6f97dba0 | aliguori | return s->max_size;
|
1544 | 6f97dba0 | aliguori | } |
1545 | 6f97dba0 | aliguori | |
1546 | 6f97dba0 | aliguori | static void win_chr_readfile(CharDriverState *chr) |
1547 | 6f97dba0 | aliguori | { |
1548 | 6f97dba0 | aliguori | WinCharState *s = chr->opaque; |
1549 | 6f97dba0 | aliguori | int ret, err;
|
1550 | 9bd7854e | Amit Shah | uint8_t buf[READ_BUF_LEN]; |
1551 | 6f97dba0 | aliguori | DWORD size; |
1552 | 6f97dba0 | aliguori | |
1553 | 6f97dba0 | aliguori | ZeroMemory(&s->orecv, sizeof(s->orecv));
|
1554 | 6f97dba0 | aliguori | s->orecv.hEvent = s->hrecv; |
1555 | 6f97dba0 | aliguori | ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); |
1556 | 6f97dba0 | aliguori | if (!ret) {
|
1557 | 6f97dba0 | aliguori | err = GetLastError(); |
1558 | 6f97dba0 | aliguori | if (err == ERROR_IO_PENDING) {
|
1559 | 6f97dba0 | aliguori | ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); |
1560 | 6f97dba0 | aliguori | } |
1561 | 6f97dba0 | aliguori | } |
1562 | 6f97dba0 | aliguori | |
1563 | 6f97dba0 | aliguori | if (size > 0) { |
1564 | 6f97dba0 | aliguori | qemu_chr_read(chr, buf, size); |
1565 | 6f97dba0 | aliguori | } |
1566 | 6f97dba0 | aliguori | } |
1567 | 6f97dba0 | aliguori | |
1568 | 6f97dba0 | aliguori | static void win_chr_read(CharDriverState *chr) |
1569 | 6f97dba0 | aliguori | { |
1570 | 6f97dba0 | aliguori | WinCharState *s = chr->opaque; |
1571 | 6f97dba0 | aliguori | |
1572 | 6f97dba0 | aliguori | if (s->len > s->max_size)
|
1573 | 6f97dba0 | aliguori | s->len = s->max_size; |
1574 | 6f97dba0 | aliguori | if (s->len == 0) |
1575 | 6f97dba0 | aliguori | return;
|
1576 | 6f97dba0 | aliguori | |
1577 | 6f97dba0 | aliguori | win_chr_readfile(chr); |
1578 | 6f97dba0 | aliguori | } |
1579 | 6f97dba0 | aliguori | |
1580 | 6f97dba0 | aliguori | static int win_chr_poll(void *opaque) |
1581 | 6f97dba0 | aliguori | { |
1582 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
1583 | 6f97dba0 | aliguori | WinCharState *s = chr->opaque; |
1584 | 6f97dba0 | aliguori | COMSTAT status; |
1585 | 6f97dba0 | aliguori | DWORD comerr; |
1586 | 6f97dba0 | aliguori | |
1587 | 6f97dba0 | aliguori | ClearCommError(s->hcom, &comerr, &status); |
1588 | 6f97dba0 | aliguori | if (status.cbInQue > 0) { |
1589 | 6f97dba0 | aliguori | s->len = status.cbInQue; |
1590 | 6f97dba0 | aliguori | win_chr_read_poll(chr); |
1591 | 6f97dba0 | aliguori | win_chr_read(chr); |
1592 | 6f97dba0 | aliguori | return 1; |
1593 | 6f97dba0 | aliguori | } |
1594 | 6f97dba0 | aliguori | return 0; |
1595 | 6f97dba0 | aliguori | } |
1596 | 6f97dba0 | aliguori | |
1597 | 48b76496 | Gerd Hoffmann | static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
|
1598 | 6f97dba0 | aliguori | { |
1599 | 48b76496 | Gerd Hoffmann | const char *filename = qemu_opt_get(opts, "path"); |
1600 | 6f97dba0 | aliguori | CharDriverState *chr; |
1601 | 6f97dba0 | aliguori | WinCharState *s; |
1602 | 6f97dba0 | aliguori | |
1603 | 6f97dba0 | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
1604 | 6f97dba0 | aliguori | s = qemu_mallocz(sizeof(WinCharState));
|
1605 | 6f97dba0 | aliguori | chr->opaque = s; |
1606 | 6f97dba0 | aliguori | chr->chr_write = win_chr_write; |
1607 | 6f97dba0 | aliguori | chr->chr_close = win_chr_close; |
1608 | 6f97dba0 | aliguori | |
1609 | 6f97dba0 | aliguori | if (win_chr_init(chr, filename) < 0) { |
1610 | 6f97dba0 | aliguori | free(s); |
1611 | 6f97dba0 | aliguori | free(chr); |
1612 | 6f97dba0 | aliguori | return NULL; |
1613 | 6f97dba0 | aliguori | } |
1614 | 127338e6 | Amit Shah | qemu_chr_generic_open(chr); |
1615 | 6f97dba0 | aliguori | return chr;
|
1616 | 6f97dba0 | aliguori | } |
1617 | 6f97dba0 | aliguori | |
1618 | 6f97dba0 | aliguori | static int win_chr_pipe_poll(void *opaque) |
1619 | 6f97dba0 | aliguori | { |
1620 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
1621 | 6f97dba0 | aliguori | WinCharState *s = chr->opaque; |
1622 | 6f97dba0 | aliguori | DWORD size; |
1623 | 6f97dba0 | aliguori | |
1624 | 6f97dba0 | aliguori | PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); |
1625 | 6f97dba0 | aliguori | if (size > 0) { |
1626 | 6f97dba0 | aliguori | s->len = size; |
1627 | 6f97dba0 | aliguori | win_chr_read_poll(chr); |
1628 | 6f97dba0 | aliguori | win_chr_read(chr); |
1629 | 6f97dba0 | aliguori | return 1; |
1630 | 6f97dba0 | aliguori | } |
1631 | 6f97dba0 | aliguori | return 0; |
1632 | 6f97dba0 | aliguori | } |
1633 | 6f97dba0 | aliguori | |
1634 | 6f97dba0 | aliguori | static int win_chr_pipe_init(CharDriverState *chr, const char *filename) |
1635 | 6f97dba0 | aliguori | { |
1636 | 6f97dba0 | aliguori | WinCharState *s = chr->opaque; |
1637 | 6f97dba0 | aliguori | OVERLAPPED ov; |
1638 | 6f97dba0 | aliguori | int ret;
|
1639 | 6f97dba0 | aliguori | DWORD size; |
1640 | 6f97dba0 | aliguori | char openname[256]; |
1641 | 6f97dba0 | aliguori | |
1642 | 6f97dba0 | aliguori | s->fpipe = TRUE; |
1643 | 6f97dba0 | aliguori | |
1644 | 6f97dba0 | aliguori | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
1645 | 6f97dba0 | aliguori | if (!s->hsend) {
|
1646 | 6f97dba0 | aliguori | fprintf(stderr, "Failed CreateEvent\n");
|
1647 | 6f97dba0 | aliguori | goto fail;
|
1648 | 6f97dba0 | aliguori | } |
1649 | 6f97dba0 | aliguori | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
1650 | 6f97dba0 | aliguori | if (!s->hrecv) {
|
1651 | 6f97dba0 | aliguori | fprintf(stderr, "Failed CreateEvent\n");
|
1652 | 6f97dba0 | aliguori | goto fail;
|
1653 | 6f97dba0 | aliguori | } |
1654 | 6f97dba0 | aliguori | |
1655 | 6f97dba0 | aliguori | snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename); |
1656 | 6f97dba0 | aliguori | s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, |
1657 | 6f97dba0 | aliguori | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | |
1658 | 6f97dba0 | aliguori | PIPE_WAIT, |
1659 | 6f97dba0 | aliguori | MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
|
1660 | 6f97dba0 | aliguori | if (s->hcom == INVALID_HANDLE_VALUE) {
|
1661 | 6f97dba0 | aliguori | fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
|
1662 | 6f97dba0 | aliguori | s->hcom = NULL;
|
1663 | 6f97dba0 | aliguori | goto fail;
|
1664 | 6f97dba0 | aliguori | } |
1665 | 6f97dba0 | aliguori | |
1666 | 6f97dba0 | aliguori | ZeroMemory(&ov, sizeof(ov));
|
1667 | 6f97dba0 | aliguori | ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
1668 | 6f97dba0 | aliguori | ret = ConnectNamedPipe(s->hcom, &ov); |
1669 | 6f97dba0 | aliguori | if (ret) {
|
1670 | 6f97dba0 | aliguori | fprintf(stderr, "Failed ConnectNamedPipe\n");
|
1671 | 6f97dba0 | aliguori | goto fail;
|
1672 | 6f97dba0 | aliguori | } |
1673 | 6f97dba0 | aliguori | |
1674 | 6f97dba0 | aliguori | ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); |
1675 | 6f97dba0 | aliguori | if (!ret) {
|
1676 | 6f97dba0 | aliguori | fprintf(stderr, "Failed GetOverlappedResult\n");
|
1677 | 6f97dba0 | aliguori | if (ov.hEvent) {
|
1678 | 6f97dba0 | aliguori | CloseHandle(ov.hEvent); |
1679 | 6f97dba0 | aliguori | ov.hEvent = NULL;
|
1680 | 6f97dba0 | aliguori | } |
1681 | 6f97dba0 | aliguori | goto fail;
|
1682 | 6f97dba0 | aliguori | } |
1683 | 6f97dba0 | aliguori | |
1684 | 6f97dba0 | aliguori | if (ov.hEvent) {
|
1685 | 6f97dba0 | aliguori | CloseHandle(ov.hEvent); |
1686 | 6f97dba0 | aliguori | ov.hEvent = NULL;
|
1687 | 6f97dba0 | aliguori | } |
1688 | 6f97dba0 | aliguori | qemu_add_polling_cb(win_chr_pipe_poll, chr); |
1689 | 6f97dba0 | aliguori | return 0; |
1690 | 6f97dba0 | aliguori | |
1691 | 6f97dba0 | aliguori | fail:
|
1692 | 6f97dba0 | aliguori | win_chr_close(chr); |
1693 | 6f97dba0 | aliguori | return -1; |
1694 | 6f97dba0 | aliguori | } |
1695 | 6f97dba0 | aliguori | |
1696 | 6f97dba0 | aliguori | |
1697 | 7d31544f | Gerd Hoffmann | static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
|
1698 | 6f97dba0 | aliguori | { |
1699 | 7d31544f | Gerd Hoffmann | const char *filename = qemu_opt_get(opts, "path"); |
1700 | 6f97dba0 | aliguori | CharDriverState *chr; |
1701 | 6f97dba0 | aliguori | WinCharState *s; |
1702 | 6f97dba0 | aliguori | |
1703 | 6f97dba0 | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
1704 | 6f97dba0 | aliguori | s = qemu_mallocz(sizeof(WinCharState));
|
1705 | 6f97dba0 | aliguori | chr->opaque = s; |
1706 | 6f97dba0 | aliguori | chr->chr_write = win_chr_write; |
1707 | 6f97dba0 | aliguori | chr->chr_close = win_chr_close; |
1708 | 6f97dba0 | aliguori | |
1709 | 6f97dba0 | aliguori | if (win_chr_pipe_init(chr, filename) < 0) { |
1710 | 6f97dba0 | aliguori | free(s); |
1711 | 6f97dba0 | aliguori | free(chr); |
1712 | 6f97dba0 | aliguori | return NULL; |
1713 | 6f97dba0 | aliguori | } |
1714 | 127338e6 | Amit Shah | qemu_chr_generic_open(chr); |
1715 | 6f97dba0 | aliguori | return chr;
|
1716 | 6f97dba0 | aliguori | } |
1717 | 6f97dba0 | aliguori | |
1718 | 6f97dba0 | aliguori | static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
|
1719 | 6f97dba0 | aliguori | { |
1720 | 6f97dba0 | aliguori | CharDriverState *chr; |
1721 | 6f97dba0 | aliguori | WinCharState *s; |
1722 | 6f97dba0 | aliguori | |
1723 | 6f97dba0 | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
1724 | 6f97dba0 | aliguori | s = qemu_mallocz(sizeof(WinCharState));
|
1725 | 6f97dba0 | aliguori | s->hcom = fd_out; |
1726 | 6f97dba0 | aliguori | chr->opaque = s; |
1727 | 6f97dba0 | aliguori | chr->chr_write = win_chr_write; |
1728 | 127338e6 | Amit Shah | qemu_chr_generic_open(chr); |
1729 | 6f97dba0 | aliguori | return chr;
|
1730 | 6f97dba0 | aliguori | } |
1731 | 6f97dba0 | aliguori | |
1732 | d6c983cd | Gerd Hoffmann | static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
|
1733 | 6f97dba0 | aliguori | { |
1734 | 6f97dba0 | aliguori | return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
|
1735 | 6f97dba0 | aliguori | } |
1736 | 6f97dba0 | aliguori | |
1737 | 7d31544f | Gerd Hoffmann | static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
|
1738 | 6f97dba0 | aliguori | { |
1739 | 7d31544f | Gerd Hoffmann | const char *file_out = qemu_opt_get(opts, "path"); |
1740 | 6f97dba0 | aliguori | HANDLE fd_out; |
1741 | 6f97dba0 | aliguori | |
1742 | 6f97dba0 | aliguori | fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
|
1743 | 6f97dba0 | aliguori | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
1744 | 6f97dba0 | aliguori | if (fd_out == INVALID_HANDLE_VALUE)
|
1745 | 6f97dba0 | aliguori | return NULL; |
1746 | 6f97dba0 | aliguori | |
1747 | 6f97dba0 | aliguori | return qemu_chr_open_win_file(fd_out);
|
1748 | 6f97dba0 | aliguori | } |
1749 | 6f97dba0 | aliguori | #endif /* !_WIN32 */ |
1750 | 6f97dba0 | aliguori | |
1751 | 6f97dba0 | aliguori | /***********************************************************/
|
1752 | 6f97dba0 | aliguori | /* UDP Net console */
|
1753 | 6f97dba0 | aliguori | |
1754 | 6f97dba0 | aliguori | typedef struct { |
1755 | 6f97dba0 | aliguori | int fd;
|
1756 | 9bd7854e | Amit Shah | uint8_t buf[READ_BUF_LEN]; |
1757 | 6f97dba0 | aliguori | int bufcnt;
|
1758 | 6f97dba0 | aliguori | int bufptr;
|
1759 | 6f97dba0 | aliguori | int max_size;
|
1760 | 6f97dba0 | aliguori | } NetCharDriver; |
1761 | 6f97dba0 | aliguori | |
1762 | 6f97dba0 | aliguori | static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1763 | 6f97dba0 | aliguori | { |
1764 | 6f97dba0 | aliguori | NetCharDriver *s = chr->opaque; |
1765 | 6f97dba0 | aliguori | |
1766 | 7e1b35b4 | Gerd Hoffmann | return send(s->fd, (const void *)buf, len, 0); |
1767 | 6f97dba0 | aliguori | } |
1768 | 6f97dba0 | aliguori | |
1769 | 6f97dba0 | aliguori | static int udp_chr_read_poll(void *opaque) |
1770 | 6f97dba0 | aliguori | { |
1771 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
1772 | 6f97dba0 | aliguori | NetCharDriver *s = chr->opaque; |
1773 | 6f97dba0 | aliguori | |
1774 | 6f97dba0 | aliguori | s->max_size = qemu_chr_can_read(chr); |
1775 | 6f97dba0 | aliguori | |
1776 | 6f97dba0 | aliguori | /* If there were any stray characters in the queue process them
|
1777 | 6f97dba0 | aliguori | * first
|
1778 | 6f97dba0 | aliguori | */
|
1779 | 6f97dba0 | aliguori | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
1780 | 6f97dba0 | aliguori | qemu_chr_read(chr, &s->buf[s->bufptr], 1);
|
1781 | 6f97dba0 | aliguori | s->bufptr++; |
1782 | 6f97dba0 | aliguori | s->max_size = qemu_chr_can_read(chr); |
1783 | 6f97dba0 | aliguori | } |
1784 | 6f97dba0 | aliguori | return s->max_size;
|
1785 | 6f97dba0 | aliguori | } |
1786 | 6f97dba0 | aliguori | |
1787 | 6f97dba0 | aliguori | static void udp_chr_read(void *opaque) |
1788 | 6f97dba0 | aliguori | { |
1789 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
1790 | 6f97dba0 | aliguori | NetCharDriver *s = chr->opaque; |
1791 | 6f97dba0 | aliguori | |
1792 | 6f97dba0 | aliguori | if (s->max_size == 0) |
1793 | 6f97dba0 | aliguori | return;
|
1794 | c5b76b38 | Blue Swirl | s->bufcnt = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0); |
1795 | 6f97dba0 | aliguori | s->bufptr = s->bufcnt; |
1796 | 6f97dba0 | aliguori | if (s->bufcnt <= 0) |
1797 | 6f97dba0 | aliguori | return;
|
1798 | 6f97dba0 | aliguori | |
1799 | 6f97dba0 | aliguori | s->bufptr = 0;
|
1800 | 6f97dba0 | aliguori | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
1801 | 6f97dba0 | aliguori | qemu_chr_read(chr, &s->buf[s->bufptr], 1);
|
1802 | 6f97dba0 | aliguori | s->bufptr++; |
1803 | 6f97dba0 | aliguori | s->max_size = qemu_chr_can_read(chr); |
1804 | 6f97dba0 | aliguori | } |
1805 | 6f97dba0 | aliguori | } |
1806 | 6f97dba0 | aliguori | |
1807 | 6f97dba0 | aliguori | static void udp_chr_update_read_handler(CharDriverState *chr) |
1808 | 6f97dba0 | aliguori | { |
1809 | 6f97dba0 | aliguori | NetCharDriver *s = chr->opaque; |
1810 | 6f97dba0 | aliguori | |
1811 | 6f97dba0 | aliguori | if (s->fd >= 0) { |
1812 | 6f97dba0 | aliguori | qemu_set_fd_handler2(s->fd, udp_chr_read_poll, |
1813 | 6f97dba0 | aliguori | udp_chr_read, NULL, chr);
|
1814 | 6f97dba0 | aliguori | } |
1815 | 6f97dba0 | aliguori | } |
1816 | 6f97dba0 | aliguori | |
1817 | 819f56b7 | aliguori | static void udp_chr_close(CharDriverState *chr) |
1818 | 819f56b7 | aliguori | { |
1819 | 819f56b7 | aliguori | NetCharDriver *s = chr->opaque; |
1820 | 819f56b7 | aliguori | if (s->fd >= 0) { |
1821 | 819f56b7 | aliguori | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
1822 | 819f56b7 | aliguori | closesocket(s->fd); |
1823 | 819f56b7 | aliguori | } |
1824 | 819f56b7 | aliguori | qemu_free(s); |
1825 | 793cbfb5 | Amit Shah | qemu_chr_event(chr, CHR_EVENT_CLOSED); |
1826 | 819f56b7 | aliguori | } |
1827 | 819f56b7 | aliguori | |
1828 | 7e1b35b4 | Gerd Hoffmann | static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
|
1829 | 6f97dba0 | aliguori | { |
1830 | 6f97dba0 | aliguori | CharDriverState *chr = NULL;
|
1831 | 6f97dba0 | aliguori | NetCharDriver *s = NULL;
|
1832 | 6f97dba0 | aliguori | int fd = -1; |
1833 | 6f97dba0 | aliguori | |
1834 | 6f97dba0 | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
1835 | 6f97dba0 | aliguori | s = qemu_mallocz(sizeof(NetCharDriver));
|
1836 | 6f97dba0 | aliguori | |
1837 | 7e1b35b4 | Gerd Hoffmann | fd = inet_dgram_opts(opts); |
1838 | 6f97dba0 | aliguori | if (fd < 0) { |
1839 | 7e1b35b4 | Gerd Hoffmann | fprintf(stderr, "inet_dgram_opts failed\n");
|
1840 | 6f97dba0 | aliguori | goto return_err;
|
1841 | 6f97dba0 | aliguori | } |
1842 | 6f97dba0 | aliguori | |
1843 | 6f97dba0 | aliguori | s->fd = fd; |
1844 | 6f97dba0 | aliguori | s->bufcnt = 0;
|
1845 | 6f97dba0 | aliguori | s->bufptr = 0;
|
1846 | 6f97dba0 | aliguori | chr->opaque = s; |
1847 | 6f97dba0 | aliguori | chr->chr_write = udp_chr_write; |
1848 | 6f97dba0 | aliguori | chr->chr_update_read_handler = udp_chr_update_read_handler; |
1849 | 819f56b7 | aliguori | chr->chr_close = udp_chr_close; |
1850 | 6f97dba0 | aliguori | return chr;
|
1851 | 6f97dba0 | aliguori | |
1852 | 6f97dba0 | aliguori | return_err:
|
1853 | 6f97dba0 | aliguori | if (chr)
|
1854 | 6f97dba0 | aliguori | free(chr); |
1855 | 6f97dba0 | aliguori | if (s)
|
1856 | 6f97dba0 | aliguori | free(s); |
1857 | 6f97dba0 | aliguori | if (fd >= 0) |
1858 | 6f97dba0 | aliguori | closesocket(fd); |
1859 | 6f97dba0 | aliguori | return NULL; |
1860 | 6f97dba0 | aliguori | } |
1861 | 6f97dba0 | aliguori | |
1862 | 6f97dba0 | aliguori | /***********************************************************/
|
1863 | 6f97dba0 | aliguori | /* TCP Net console */
|
1864 | 6f97dba0 | aliguori | |
1865 | 6f97dba0 | aliguori | typedef struct { |
1866 | 6f97dba0 | aliguori | int fd, listen_fd;
|
1867 | 6f97dba0 | aliguori | int connected;
|
1868 | 6f97dba0 | aliguori | int max_size;
|
1869 | 6f97dba0 | aliguori | int do_telnetopt;
|
1870 | 6f97dba0 | aliguori | int do_nodelay;
|
1871 | 6f97dba0 | aliguori | int is_unix;
|
1872 | 7d174059 | Mark McLoughlin | int msgfd;
|
1873 | 6f97dba0 | aliguori | } TCPCharDriver; |
1874 | 6f97dba0 | aliguori | |
1875 | 6f97dba0 | aliguori | static void tcp_chr_accept(void *opaque); |
1876 | 6f97dba0 | aliguori | |
1877 | 6f97dba0 | aliguori | static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1878 | 6f97dba0 | aliguori | { |
1879 | 6f97dba0 | aliguori | TCPCharDriver *s = chr->opaque; |
1880 | 6f97dba0 | aliguori | if (s->connected) {
|
1881 | 6f97dba0 | aliguori | return send_all(s->fd, buf, len);
|
1882 | 6f97dba0 | aliguori | } else {
|
1883 | 6f97dba0 | aliguori | /* XXX: indicate an error ? */
|
1884 | 6f97dba0 | aliguori | return len;
|
1885 | 6f97dba0 | aliguori | } |
1886 | 6f97dba0 | aliguori | } |
1887 | 6f97dba0 | aliguori | |
1888 | 6f97dba0 | aliguori | static int tcp_chr_read_poll(void *opaque) |
1889 | 6f97dba0 | aliguori | { |
1890 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
1891 | 6f97dba0 | aliguori | TCPCharDriver *s = chr->opaque; |
1892 | 6f97dba0 | aliguori | if (!s->connected)
|
1893 | 6f97dba0 | aliguori | return 0; |
1894 | 6f97dba0 | aliguori | s->max_size = qemu_chr_can_read(chr); |
1895 | 6f97dba0 | aliguori | return s->max_size;
|
1896 | 6f97dba0 | aliguori | } |
1897 | 6f97dba0 | aliguori | |
1898 | 6f97dba0 | aliguori | #define IAC 255 |
1899 | 6f97dba0 | aliguori | #define IAC_BREAK 243 |
1900 | 6f97dba0 | aliguori | static void tcp_chr_process_IAC_bytes(CharDriverState *chr, |
1901 | 6f97dba0 | aliguori | TCPCharDriver *s, |
1902 | 6f97dba0 | aliguori | uint8_t *buf, int *size)
|
1903 | 6f97dba0 | aliguori | { |
1904 | 6f97dba0 | aliguori | /* Handle any telnet client's basic IAC options to satisfy char by
|
1905 | 6f97dba0 | aliguori | * char mode with no echo. All IAC options will be removed from
|
1906 | 6f97dba0 | aliguori | * the buf and the do_telnetopt variable will be used to track the
|
1907 | 6f97dba0 | aliguori | * state of the width of the IAC information.
|
1908 | 6f97dba0 | aliguori | *
|
1909 | 6f97dba0 | aliguori | * IAC commands come in sets of 3 bytes with the exception of the
|
1910 | 6f97dba0 | aliguori | * "IAC BREAK" command and the double IAC.
|
1911 | 6f97dba0 | aliguori | */
|
1912 | 6f97dba0 | aliguori | |
1913 | 6f97dba0 | aliguori | int i;
|
1914 | 6f97dba0 | aliguori | int j = 0; |
1915 | 6f97dba0 | aliguori | |
1916 | 6f97dba0 | aliguori | for (i = 0; i < *size; i++) { |
1917 | 6f97dba0 | aliguori | if (s->do_telnetopt > 1) { |
1918 | 6f97dba0 | aliguori | if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { |
1919 | 6f97dba0 | aliguori | /* Double IAC means send an IAC */
|
1920 | 6f97dba0 | aliguori | if (j != i)
|
1921 | 6f97dba0 | aliguori | buf[j] = buf[i]; |
1922 | 6f97dba0 | aliguori | j++; |
1923 | 6f97dba0 | aliguori | s->do_telnetopt = 1;
|
1924 | 6f97dba0 | aliguori | } else {
|
1925 | 6f97dba0 | aliguori | if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { |
1926 | 6f97dba0 | aliguori | /* Handle IAC break commands by sending a serial break */
|
1927 | 6f97dba0 | aliguori | qemu_chr_event(chr, CHR_EVENT_BREAK); |
1928 | 6f97dba0 | aliguori | s->do_telnetopt++; |
1929 | 6f97dba0 | aliguori | } |
1930 | 6f97dba0 | aliguori | s->do_telnetopt++; |
1931 | 6f97dba0 | aliguori | } |
1932 | 6f97dba0 | aliguori | if (s->do_telnetopt >= 4) { |
1933 | 6f97dba0 | aliguori | s->do_telnetopt = 1;
|
1934 | 6f97dba0 | aliguori | } |
1935 | 6f97dba0 | aliguori | } else {
|
1936 | 6f97dba0 | aliguori | if ((unsigned char)buf[i] == IAC) { |
1937 | 6f97dba0 | aliguori | s->do_telnetopt = 2;
|
1938 | 6f97dba0 | aliguori | } else {
|
1939 | 6f97dba0 | aliguori | if (j != i)
|
1940 | 6f97dba0 | aliguori | buf[j] = buf[i]; |
1941 | 6f97dba0 | aliguori | j++; |
1942 | 6f97dba0 | aliguori | } |
1943 | 6f97dba0 | aliguori | } |
1944 | 6f97dba0 | aliguori | } |
1945 | 6f97dba0 | aliguori | *size = j; |
1946 | 6f97dba0 | aliguori | } |
1947 | 6f97dba0 | aliguori | |
1948 | 7d174059 | Mark McLoughlin | static int tcp_get_msgfd(CharDriverState *chr) |
1949 | 7d174059 | Mark McLoughlin | { |
1950 | 7d174059 | Mark McLoughlin | TCPCharDriver *s = chr->opaque; |
1951 | 7d174059 | Mark McLoughlin | |
1952 | 7d174059 | Mark McLoughlin | return s->msgfd;
|
1953 | 7d174059 | Mark McLoughlin | } |
1954 | 7d174059 | Mark McLoughlin | |
1955 | 73bcc2ac | Anthony Liguori | #ifndef _WIN32
|
1956 | 7d174059 | Mark McLoughlin | static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg) |
1957 | 7d174059 | Mark McLoughlin | { |
1958 | 7d174059 | Mark McLoughlin | TCPCharDriver *s = chr->opaque; |
1959 | 7d174059 | Mark McLoughlin | struct cmsghdr *cmsg;
|
1960 | 7d174059 | Mark McLoughlin | |
1961 | 7d174059 | Mark McLoughlin | for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
|
1962 | 7d174059 | Mark McLoughlin | int fd;
|
1963 | 7d174059 | Mark McLoughlin | |
1964 | 7d174059 | Mark McLoughlin | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) || |
1965 | 7d174059 | Mark McLoughlin | cmsg->cmsg_level != SOL_SOCKET || |
1966 | 7d174059 | Mark McLoughlin | cmsg->cmsg_type != SCM_RIGHTS) |
1967 | 7d174059 | Mark McLoughlin | continue;
|
1968 | 7d174059 | Mark McLoughlin | |
1969 | 7d174059 | Mark McLoughlin | fd = *((int *)CMSG_DATA(cmsg));
|
1970 | 7d174059 | Mark McLoughlin | if (fd < 0) |
1971 | 7d174059 | Mark McLoughlin | continue;
|
1972 | 7d174059 | Mark McLoughlin | |
1973 | 7d174059 | Mark McLoughlin | if (s->msgfd != -1) |
1974 | 7d174059 | Mark McLoughlin | close(s->msgfd); |
1975 | 7d174059 | Mark McLoughlin | s->msgfd = fd; |
1976 | 7d174059 | Mark McLoughlin | } |
1977 | 7d174059 | Mark McLoughlin | } |
1978 | 7d174059 | Mark McLoughlin | |
1979 | 9977c894 | Mark McLoughlin | static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len) |
1980 | 9977c894 | Mark McLoughlin | { |
1981 | 9977c894 | Mark McLoughlin | TCPCharDriver *s = chr->opaque; |
1982 | 7cba04f6 | Blue Swirl | struct msghdr msg = { NULL, }; |
1983 | 9977c894 | Mark McLoughlin | struct iovec iov[1]; |
1984 | 7d174059 | Mark McLoughlin | union {
|
1985 | 7d174059 | Mark McLoughlin | struct cmsghdr cmsg;
|
1986 | 7d174059 | Mark McLoughlin | char control[CMSG_SPACE(sizeof(int))]; |
1987 | 7d174059 | Mark McLoughlin | } msg_control; |
1988 | 7d174059 | Mark McLoughlin | ssize_t ret; |
1989 | 9977c894 | Mark McLoughlin | |
1990 | 9977c894 | Mark McLoughlin | iov[0].iov_base = buf;
|
1991 | 9977c894 | Mark McLoughlin | iov[0].iov_len = len;
|
1992 | 9977c894 | Mark McLoughlin | |
1993 | 9977c894 | Mark McLoughlin | msg.msg_iov = iov; |
1994 | 9977c894 | Mark McLoughlin | msg.msg_iovlen = 1;
|
1995 | 7d174059 | Mark McLoughlin | msg.msg_control = &msg_control; |
1996 | 7d174059 | Mark McLoughlin | msg.msg_controllen = sizeof(msg_control);
|
1997 | 7d174059 | Mark McLoughlin | |
1998 | 7d174059 | Mark McLoughlin | ret = recvmsg(s->fd, &msg, 0);
|
1999 | 7d174059 | Mark McLoughlin | if (ret > 0 && s->is_unix) |
2000 | 7d174059 | Mark McLoughlin | unix_process_msgfd(chr, &msg); |
2001 | 9977c894 | Mark McLoughlin | |
2002 | 7d174059 | Mark McLoughlin | return ret;
|
2003 | 9977c894 | Mark McLoughlin | } |
2004 | 9977c894 | Mark McLoughlin | #else
|
2005 | 9977c894 | Mark McLoughlin | static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len) |
2006 | 9977c894 | Mark McLoughlin | { |
2007 | 9977c894 | Mark McLoughlin | TCPCharDriver *s = chr->opaque; |
2008 | 9977c894 | Mark McLoughlin | return recv(s->fd, buf, len, 0); |
2009 | 9977c894 | Mark McLoughlin | } |
2010 | 9977c894 | Mark McLoughlin | #endif
|
2011 | 9977c894 | Mark McLoughlin | |
2012 | 6f97dba0 | aliguori | static void tcp_chr_read(void *opaque) |
2013 | 6f97dba0 | aliguori | { |
2014 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
2015 | 6f97dba0 | aliguori | TCPCharDriver *s = chr->opaque; |
2016 | 9bd7854e | Amit Shah | uint8_t buf[READ_BUF_LEN]; |
2017 | 6f97dba0 | aliguori | int len, size;
|
2018 | 6f97dba0 | aliguori | |
2019 | 6f97dba0 | aliguori | if (!s->connected || s->max_size <= 0) |
2020 | 6f97dba0 | aliguori | return;
|
2021 | 6f97dba0 | aliguori | len = sizeof(buf);
|
2022 | 6f97dba0 | aliguori | if (len > s->max_size)
|
2023 | 6f97dba0 | aliguori | len = s->max_size; |
2024 | 9977c894 | Mark McLoughlin | size = tcp_chr_recv(chr, (void *)buf, len);
|
2025 | 6f97dba0 | aliguori | if (size == 0) { |
2026 | 6f97dba0 | aliguori | /* connection closed */
|
2027 | 6f97dba0 | aliguori | s->connected = 0;
|
2028 | 6f97dba0 | aliguori | if (s->listen_fd >= 0) { |
2029 | 6f97dba0 | aliguori | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
|
2030 | 6f97dba0 | aliguori | } |
2031 | 6f97dba0 | aliguori | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
2032 | 6f97dba0 | aliguori | closesocket(s->fd); |
2033 | 6f97dba0 | aliguori | s->fd = -1;
|
2034 | 793cbfb5 | Amit Shah | qemu_chr_event(chr, CHR_EVENT_CLOSED); |
2035 | 6f97dba0 | aliguori | } else if (size > 0) { |
2036 | 6f97dba0 | aliguori | if (s->do_telnetopt)
|
2037 | 6f97dba0 | aliguori | tcp_chr_process_IAC_bytes(chr, s, buf, &size); |
2038 | 6f97dba0 | aliguori | if (size > 0) |
2039 | 6f97dba0 | aliguori | qemu_chr_read(chr, buf, size); |
2040 | 7d174059 | Mark McLoughlin | if (s->msgfd != -1) { |
2041 | 7d174059 | Mark McLoughlin | close(s->msgfd); |
2042 | 7d174059 | Mark McLoughlin | s->msgfd = -1;
|
2043 | 7d174059 | Mark McLoughlin | } |
2044 | 6f97dba0 | aliguori | } |
2045 | 6f97dba0 | aliguori | } |
2046 | 6f97dba0 | aliguori | |
2047 | 6f97dba0 | aliguori | static void tcp_chr_connect(void *opaque) |
2048 | 6f97dba0 | aliguori | { |
2049 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
2050 | 6f97dba0 | aliguori | TCPCharDriver *s = chr->opaque; |
2051 | 6f97dba0 | aliguori | |
2052 | 6f97dba0 | aliguori | s->connected = 1;
|
2053 | 6f97dba0 | aliguori | qemu_set_fd_handler2(s->fd, tcp_chr_read_poll, |
2054 | 6f97dba0 | aliguori | tcp_chr_read, NULL, chr);
|
2055 | 127338e6 | Amit Shah | qemu_chr_generic_open(chr); |
2056 | 6f97dba0 | aliguori | } |
2057 | 6f97dba0 | aliguori | |
2058 | 6f97dba0 | aliguori | #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; |
2059 | 6f97dba0 | aliguori | static void tcp_chr_telnet_init(int fd) |
2060 | 6f97dba0 | aliguori | { |
2061 | 6f97dba0 | aliguori | char buf[3]; |
2062 | 6f97dba0 | aliguori | /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
|
2063 | 6f97dba0 | aliguori | IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ |
2064 | 6f97dba0 | aliguori | send(fd, (char *)buf, 3, 0); |
2065 | 6f97dba0 | aliguori | IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ |
2066 | 6f97dba0 | aliguori | send(fd, (char *)buf, 3, 0); |
2067 | 6f97dba0 | aliguori | IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ |
2068 | 6f97dba0 | aliguori | send(fd, (char *)buf, 3, 0); |
2069 | 6f97dba0 | aliguori | IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ |
2070 | 6f97dba0 | aliguori | send(fd, (char *)buf, 3, 0); |
2071 | 6f97dba0 | aliguori | } |
2072 | 6f97dba0 | aliguori | |
2073 | 6f97dba0 | aliguori | static void socket_set_nodelay(int fd) |
2074 | 6f97dba0 | aliguori | { |
2075 | 6f97dba0 | aliguori | int val = 1; |
2076 | 6f97dba0 | aliguori | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
2077 | 6f97dba0 | aliguori | } |
2078 | 6f97dba0 | aliguori | |
2079 | 6f97dba0 | aliguori | static void tcp_chr_accept(void *opaque) |
2080 | 6f97dba0 | aliguori | { |
2081 | 6f97dba0 | aliguori | CharDriverState *chr = opaque; |
2082 | 6f97dba0 | aliguori | TCPCharDriver *s = chr->opaque; |
2083 | 6f97dba0 | aliguori | struct sockaddr_in saddr;
|
2084 | 6f97dba0 | aliguori | #ifndef _WIN32
|
2085 | 6f97dba0 | aliguori | struct sockaddr_un uaddr;
|
2086 | 6f97dba0 | aliguori | #endif
|
2087 | 6f97dba0 | aliguori | struct sockaddr *addr;
|
2088 | 6f97dba0 | aliguori | socklen_t len; |
2089 | 6f97dba0 | aliguori | int fd;
|
2090 | 6f97dba0 | aliguori | |
2091 | 6f97dba0 | aliguori | for(;;) {
|
2092 | 6f97dba0 | aliguori | #ifndef _WIN32
|
2093 | 6f97dba0 | aliguori | if (s->is_unix) {
|
2094 | 6f97dba0 | aliguori | len = sizeof(uaddr);
|
2095 | 6f97dba0 | aliguori | addr = (struct sockaddr *)&uaddr;
|
2096 | 6f97dba0 | aliguori | } else
|
2097 | 6f97dba0 | aliguori | #endif
|
2098 | 6f97dba0 | aliguori | { |
2099 | 6f97dba0 | aliguori | len = sizeof(saddr);
|
2100 | 6f97dba0 | aliguori | addr = (struct sockaddr *)&saddr;
|
2101 | 6f97dba0 | aliguori | } |
2102 | 6f97dba0 | aliguori | fd = accept(s->listen_fd, addr, &len); |
2103 | 6f97dba0 | aliguori | if (fd < 0 && errno != EINTR) { |
2104 | 6f97dba0 | aliguori | return;
|
2105 | 6f97dba0 | aliguori | } else if (fd >= 0) { |
2106 | 6f97dba0 | aliguori | if (s->do_telnetopt)
|
2107 | 6f97dba0 | aliguori | tcp_chr_telnet_init(fd); |
2108 | 6f97dba0 | aliguori | break;
|
2109 | 6f97dba0 | aliguori | } |
2110 | 6f97dba0 | aliguori | } |
2111 | 6f97dba0 | aliguori | socket_set_nonblock(fd); |
2112 | 6f97dba0 | aliguori | if (s->do_nodelay)
|
2113 | 6f97dba0 | aliguori | socket_set_nodelay(fd); |
2114 | 6f97dba0 | aliguori | s->fd = fd; |
2115 | 6f97dba0 | aliguori | qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); |
2116 | 6f97dba0 | aliguori | tcp_chr_connect(chr); |
2117 | 6f97dba0 | aliguori | } |
2118 | 6f97dba0 | aliguori | |
2119 | 6f97dba0 | aliguori | static void tcp_chr_close(CharDriverState *chr) |
2120 | 6f97dba0 | aliguori | { |
2121 | 6f97dba0 | aliguori | TCPCharDriver *s = chr->opaque; |
2122 | 819f56b7 | aliguori | if (s->fd >= 0) { |
2123 | 819f56b7 | aliguori | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
2124 | 6f97dba0 | aliguori | closesocket(s->fd); |
2125 | 819f56b7 | aliguori | } |
2126 | 819f56b7 | aliguori | if (s->listen_fd >= 0) { |
2127 | 819f56b7 | aliguori | qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); |
2128 | 6f97dba0 | aliguori | closesocket(s->listen_fd); |
2129 | 819f56b7 | aliguori | } |
2130 | 6f97dba0 | aliguori | qemu_free(s); |
2131 | 793cbfb5 | Amit Shah | qemu_chr_event(chr, CHR_EVENT_CLOSED); |
2132 | 6f97dba0 | aliguori | } |
2133 | 6f97dba0 | aliguori | |
2134 | aeb2c47a | Gerd Hoffmann | static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
|
2135 | 6f97dba0 | aliguori | { |
2136 | 6f97dba0 | aliguori | CharDriverState *chr = NULL;
|
2137 | 6f97dba0 | aliguori | TCPCharDriver *s = NULL;
|
2138 | aeb2c47a | Gerd Hoffmann | int fd = -1; |
2139 | aeb2c47a | Gerd Hoffmann | int is_listen;
|
2140 | aeb2c47a | Gerd Hoffmann | int is_waitconnect;
|
2141 | aeb2c47a | Gerd Hoffmann | int do_nodelay;
|
2142 | aeb2c47a | Gerd Hoffmann | int is_unix;
|
2143 | aeb2c47a | Gerd Hoffmann | int is_telnet;
|
2144 | aeb2c47a | Gerd Hoffmann | |
2145 | aeb2c47a | Gerd Hoffmann | is_listen = qemu_opt_get_bool(opts, "server", 0); |
2146 | aeb2c47a | Gerd Hoffmann | is_waitconnect = qemu_opt_get_bool(opts, "wait", 1); |
2147 | aeb2c47a | Gerd Hoffmann | is_telnet = qemu_opt_get_bool(opts, "telnet", 0); |
2148 | aeb2c47a | Gerd Hoffmann | do_nodelay = !qemu_opt_get_bool(opts, "delay", 1); |
2149 | aeb2c47a | Gerd Hoffmann | is_unix = qemu_opt_get(opts, "path") != NULL; |
2150 | 6f97dba0 | aliguori | if (!is_listen)
|
2151 | 6f97dba0 | aliguori | is_waitconnect = 0;
|
2152 | 6f97dba0 | aliguori | |
2153 | 6f97dba0 | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
2154 | 6f97dba0 | aliguori | s = qemu_mallocz(sizeof(TCPCharDriver));
|
2155 | 6f97dba0 | aliguori | |
2156 | f07b6003 | aliguori | if (is_unix) {
|
2157 | f07b6003 | aliguori | if (is_listen) {
|
2158 | aeb2c47a | Gerd Hoffmann | fd = unix_listen_opts(opts); |
2159 | f07b6003 | aliguori | } else {
|
2160 | aeb2c47a | Gerd Hoffmann | fd = unix_connect_opts(opts); |
2161 | f07b6003 | aliguori | } |
2162 | f07b6003 | aliguori | } else {
|
2163 | f07b6003 | aliguori | if (is_listen) {
|
2164 | aeb2c47a | Gerd Hoffmann | fd = inet_listen_opts(opts, 0);
|
2165 | f07b6003 | aliguori | } else {
|
2166 | aeb2c47a | Gerd Hoffmann | fd = inet_connect_opts(opts); |
2167 | f07b6003 | aliguori | } |
2168 | f07b6003 | aliguori | } |
2169 | 6f97dba0 | aliguori | if (fd < 0) |
2170 | 6f97dba0 | aliguori | goto fail;
|
2171 | 6f97dba0 | aliguori | |
2172 | 6f97dba0 | aliguori | if (!is_waitconnect)
|
2173 | 6f97dba0 | aliguori | socket_set_nonblock(fd); |
2174 | 6f97dba0 | aliguori | |
2175 | 6f97dba0 | aliguori | s->connected = 0;
|
2176 | 6f97dba0 | aliguori | s->fd = -1;
|
2177 | 6f97dba0 | aliguori | s->listen_fd = -1;
|
2178 | 7d174059 | Mark McLoughlin | s->msgfd = -1;
|
2179 | 6f97dba0 | aliguori | s->is_unix = is_unix; |
2180 | 6f97dba0 | aliguori | s->do_nodelay = do_nodelay && !is_unix; |
2181 | 6f97dba0 | aliguori | |
2182 | 6f97dba0 | aliguori | chr->opaque = s; |
2183 | 6f97dba0 | aliguori | chr->chr_write = tcp_chr_write; |
2184 | 6f97dba0 | aliguori | chr->chr_close = tcp_chr_close; |
2185 | 7d174059 | Mark McLoughlin | chr->get_msgfd = tcp_get_msgfd; |
2186 | 6f97dba0 | aliguori | |
2187 | 6f97dba0 | aliguori | if (is_listen) {
|
2188 | 6f97dba0 | aliguori | s->listen_fd = fd; |
2189 | 6f97dba0 | aliguori | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
|
2190 | 6f97dba0 | aliguori | if (is_telnet)
|
2191 | 6f97dba0 | aliguori | s->do_telnetopt = 1;
|
2192 | aeb2c47a | Gerd Hoffmann | |
2193 | 6f97dba0 | aliguori | } else {
|
2194 | f07b6003 | aliguori | s->connected = 1;
|
2195 | 6f97dba0 | aliguori | s->fd = fd; |
2196 | 6f97dba0 | aliguori | socket_set_nodelay(fd); |
2197 | f07b6003 | aliguori | tcp_chr_connect(chr); |
2198 | 6f97dba0 | aliguori | } |
2199 | 6f97dba0 | aliguori | |
2200 | aeb2c47a | Gerd Hoffmann | /* for "info chardev" monitor command */
|
2201 | aeb2c47a | Gerd Hoffmann | chr->filename = qemu_malloc(256);
|
2202 | aeb2c47a | Gerd Hoffmann | if (is_unix) {
|
2203 | aeb2c47a | Gerd Hoffmann | snprintf(chr->filename, 256, "unix:%s%s", |
2204 | aeb2c47a | Gerd Hoffmann | qemu_opt_get(opts, "path"),
|
2205 | aeb2c47a | Gerd Hoffmann | qemu_opt_get_bool(opts, "server", 0) ? ",server" : ""); |
2206 | aeb2c47a | Gerd Hoffmann | } else if (is_telnet) { |
2207 | aeb2c47a | Gerd Hoffmann | snprintf(chr->filename, 256, "telnet:%s:%s%s", |
2208 | aeb2c47a | Gerd Hoffmann | qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"), |
2209 | aeb2c47a | Gerd Hoffmann | qemu_opt_get_bool(opts, "server", 0) ? ",server" : ""); |
2210 | aeb2c47a | Gerd Hoffmann | } else {
|
2211 | aeb2c47a | Gerd Hoffmann | snprintf(chr->filename, 256, "tcp:%s:%s%s", |
2212 | aeb2c47a | Gerd Hoffmann | qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"), |
2213 | aeb2c47a | Gerd Hoffmann | qemu_opt_get_bool(opts, "server", 0) ? ",server" : ""); |
2214 | aeb2c47a | Gerd Hoffmann | } |
2215 | aeb2c47a | Gerd Hoffmann | |
2216 | 6f97dba0 | aliguori | if (is_listen && is_waitconnect) {
|
2217 | f07b6003 | aliguori | printf("QEMU waiting for connection on: %s\n",
|
2218 | aeb2c47a | Gerd Hoffmann | chr->filename); |
2219 | 6f97dba0 | aliguori | tcp_chr_accept(chr); |
2220 | 6f97dba0 | aliguori | socket_set_nonblock(s->listen_fd); |
2221 | 6f97dba0 | aliguori | } |
2222 | 6f97dba0 | aliguori | return chr;
|
2223 | aeb2c47a | Gerd Hoffmann | |
2224 | 6f97dba0 | aliguori | fail:
|
2225 | 6f97dba0 | aliguori | if (fd >= 0) |
2226 | 6f97dba0 | aliguori | closesocket(fd); |
2227 | 6f97dba0 | aliguori | qemu_free(s); |
2228 | 6f97dba0 | aliguori | qemu_free(chr); |
2229 | 6f97dba0 | aliguori | return NULL; |
2230 | 6f97dba0 | aliguori | } |
2231 | 6f97dba0 | aliguori | |
2232 | 191bc01b | Gerd Hoffmann | static QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename) |
2233 | 191bc01b | Gerd Hoffmann | { |
2234 | 6ea314d9 | Gerd Hoffmann | char host[65], port[33], width[8], height[8]; |
2235 | aeb2c47a | Gerd Hoffmann | int pos;
|
2236 | 7d31544f | Gerd Hoffmann | const char *p; |
2237 | 191bc01b | Gerd Hoffmann | QemuOpts *opts; |
2238 | 191bc01b | Gerd Hoffmann | |
2239 | 191bc01b | Gerd Hoffmann | opts = qemu_opts_create(&qemu_chardev_opts, label, 1);
|
2240 | 191bc01b | Gerd Hoffmann | if (NULL == opts) |
2241 | 191bc01b | Gerd Hoffmann | return NULL; |
2242 | 191bc01b | Gerd Hoffmann | |
2243 | 7591c5c1 | Gerd Hoffmann | if (strstart(filename, "mon:", &p)) { |
2244 | 7591c5c1 | Gerd Hoffmann | filename = p; |
2245 | 7591c5c1 | Gerd Hoffmann | qemu_opt_set(opts, "mux", "on"); |
2246 | 7591c5c1 | Gerd Hoffmann | } |
2247 | 7591c5c1 | Gerd Hoffmann | |
2248 | f0457e8d | Gerd Hoffmann | if (strcmp(filename, "null") == 0 || |
2249 | f0457e8d | Gerd Hoffmann | strcmp(filename, "pty") == 0 || |
2250 | f0457e8d | Gerd Hoffmann | strcmp(filename, "msmouse") == 0 || |
2251 | dc1c21e6 | Gerd Hoffmann | strcmp(filename, "braille") == 0 || |
2252 | f0457e8d | Gerd Hoffmann | strcmp(filename, "stdio") == 0) { |
2253 | 4490dadf | Gerd Hoffmann | qemu_opt_set(opts, "backend", filename);
|
2254 | 191bc01b | Gerd Hoffmann | return opts;
|
2255 | 191bc01b | Gerd Hoffmann | } |
2256 | 6ea314d9 | Gerd Hoffmann | if (strstart(filename, "vc", &p)) { |
2257 | 6ea314d9 | Gerd Hoffmann | qemu_opt_set(opts, "backend", "vc"); |
2258 | 6ea314d9 | Gerd Hoffmann | if (*p == ':') { |
2259 | 6ea314d9 | Gerd Hoffmann | if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) { |
2260 | 6ea314d9 | Gerd Hoffmann | /* pixels */
|
2261 | 6ea314d9 | Gerd Hoffmann | qemu_opt_set(opts, "width", width);
|
2262 | 6ea314d9 | Gerd Hoffmann | qemu_opt_set(opts, "height", height);
|
2263 | 6ea314d9 | Gerd Hoffmann | } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) { |
2264 | 6ea314d9 | Gerd Hoffmann | /* chars */
|
2265 | 6ea314d9 | Gerd Hoffmann | qemu_opt_set(opts, "cols", width);
|
2266 | 6ea314d9 | Gerd Hoffmann | qemu_opt_set(opts, "rows", height);
|
2267 | 6ea314d9 | Gerd Hoffmann | } else {
|
2268 | 6ea314d9 | Gerd Hoffmann | goto fail;
|
2269 | 6ea314d9 | Gerd Hoffmann | } |
2270 | 6ea314d9 | Gerd Hoffmann | } |
2271 | 6ea314d9 | Gerd Hoffmann | return opts;
|
2272 | 6ea314d9 | Gerd Hoffmann | } |
2273 | d6c983cd | Gerd Hoffmann | if (strcmp(filename, "con:") == 0) { |
2274 | d6c983cd | Gerd Hoffmann | qemu_opt_set(opts, "backend", "console"); |
2275 | d6c983cd | Gerd Hoffmann | return opts;
|
2276 | d6c983cd | Gerd Hoffmann | } |
2277 | 48b76496 | Gerd Hoffmann | if (strstart(filename, "COM", NULL)) { |
2278 | 48b76496 | Gerd Hoffmann | qemu_opt_set(opts, "backend", "serial"); |
2279 | 48b76496 | Gerd Hoffmann | qemu_opt_set(opts, "path", filename);
|
2280 | 48b76496 | Gerd Hoffmann | return opts;
|
2281 | 48b76496 | Gerd Hoffmann | } |
2282 | 7d31544f | Gerd Hoffmann | if (strstart(filename, "file:", &p)) { |
2283 | 7d31544f | Gerd Hoffmann | qemu_opt_set(opts, "backend", "file"); |
2284 | 7d31544f | Gerd Hoffmann | qemu_opt_set(opts, "path", p);
|
2285 | 7d31544f | Gerd Hoffmann | return opts;
|
2286 | 7d31544f | Gerd Hoffmann | } |
2287 | 7d31544f | Gerd Hoffmann | if (strstart(filename, "pipe:", &p)) { |
2288 | 7d31544f | Gerd Hoffmann | qemu_opt_set(opts, "backend", "pipe"); |
2289 | 7d31544f | Gerd Hoffmann | qemu_opt_set(opts, "path", p);
|
2290 | 7d31544f | Gerd Hoffmann | return opts;
|
2291 | 7d31544f | Gerd Hoffmann | } |
2292 | aeb2c47a | Gerd Hoffmann | if (strstart(filename, "tcp:", &p) || |
2293 | aeb2c47a | Gerd Hoffmann | strstart(filename, "telnet:", &p)) {
|
2294 | aeb2c47a | Gerd Hoffmann | if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { |
2295 | aeb2c47a | Gerd Hoffmann | host[0] = 0; |
2296 | aeb2c47a | Gerd Hoffmann | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) |
2297 | aeb2c47a | Gerd Hoffmann | goto fail;
|
2298 | aeb2c47a | Gerd Hoffmann | } |
2299 | aeb2c47a | Gerd Hoffmann | qemu_opt_set(opts, "backend", "socket"); |
2300 | aeb2c47a | Gerd Hoffmann | qemu_opt_set(opts, "host", host);
|
2301 | aeb2c47a | Gerd Hoffmann | qemu_opt_set(opts, "port", port);
|
2302 | aeb2c47a | Gerd Hoffmann | if (p[pos] == ',') { |
2303 | aeb2c47a | Gerd Hoffmann | if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0) |
2304 | aeb2c47a | Gerd Hoffmann | goto fail;
|
2305 | aeb2c47a | Gerd Hoffmann | } |
2306 | aeb2c47a | Gerd Hoffmann | if (strstart(filename, "telnet:", &p)) |
2307 | aeb2c47a | Gerd Hoffmann | qemu_opt_set(opts, "telnet", "on"); |
2308 | aeb2c47a | Gerd Hoffmann | return opts;
|
2309 | aeb2c47a | Gerd Hoffmann | } |
2310 | 7e1b35b4 | Gerd Hoffmann | if (strstart(filename, "udp:", &p)) { |
2311 | 7e1b35b4 | Gerd Hoffmann | qemu_opt_set(opts, "backend", "udp"); |
2312 | 7e1b35b4 | Gerd Hoffmann | if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) { |
2313 | 7e1b35b4 | Gerd Hoffmann | host[0] = 0; |
2314 | 7e1b35b4 | Gerd Hoffmann | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) { |
2315 | 7e1b35b4 | Gerd Hoffmann | fprintf(stderr, "udp #1\n");
|
2316 | 7e1b35b4 | Gerd Hoffmann | goto fail;
|
2317 | 7e1b35b4 | Gerd Hoffmann | } |
2318 | 7e1b35b4 | Gerd Hoffmann | } |
2319 | 7e1b35b4 | Gerd Hoffmann | qemu_opt_set(opts, "host", host);
|
2320 | 7e1b35b4 | Gerd Hoffmann | qemu_opt_set(opts, "port", port);
|
2321 | 7e1b35b4 | Gerd Hoffmann | if (p[pos] == '@') { |
2322 | 7e1b35b4 | Gerd Hoffmann | p += pos + 1;
|
2323 | 7e1b35b4 | Gerd Hoffmann | if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) { |
2324 | 7e1b35b4 | Gerd Hoffmann | host[0] = 0; |
2325 | 7e1b35b4 | Gerd Hoffmann | if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) { |
2326 | 7e1b35b4 | Gerd Hoffmann | fprintf(stderr, "udp #2\n");
|
2327 | 7e1b35b4 | Gerd Hoffmann | goto fail;
|
2328 | 7e1b35b4 | Gerd Hoffmann | } |
2329 | 7e1b35b4 | Gerd Hoffmann | } |
2330 | 7e1b35b4 | Gerd Hoffmann | qemu_opt_set(opts, "localaddr", host);
|
2331 | 7e1b35b4 | Gerd Hoffmann | qemu_opt_set(opts, "localport", port);
|
2332 | 7e1b35b4 | Gerd Hoffmann | } |
2333 | 7e1b35b4 | Gerd Hoffmann | return opts;
|
2334 | 7e1b35b4 | Gerd Hoffmann | } |
2335 | aeb2c47a | Gerd Hoffmann | if (strstart(filename, "unix:", &p)) { |
2336 | aeb2c47a | Gerd Hoffmann | qemu_opt_set(opts, "backend", "socket"); |
2337 | aeb2c47a | Gerd Hoffmann | if (qemu_opts_do_parse(opts, p, "path") != 0) |
2338 | aeb2c47a | Gerd Hoffmann | goto fail;
|
2339 | aeb2c47a | Gerd Hoffmann | return opts;
|
2340 | aeb2c47a | Gerd Hoffmann | } |
2341 | 48b76496 | Gerd Hoffmann | if (strstart(filename, "/dev/parport", NULL) || |
2342 | 48b76496 | Gerd Hoffmann | strstart(filename, "/dev/ppi", NULL)) { |
2343 | 48b76496 | Gerd Hoffmann | qemu_opt_set(opts, "backend", "parport"); |
2344 | 48b76496 | Gerd Hoffmann | qemu_opt_set(opts, "path", filename);
|
2345 | 48b76496 | Gerd Hoffmann | return opts;
|
2346 | 48b76496 | Gerd Hoffmann | } |
2347 | 48b76496 | Gerd Hoffmann | if (strstart(filename, "/dev/", NULL)) { |
2348 | 48b76496 | Gerd Hoffmann | qemu_opt_set(opts, "backend", "tty"); |
2349 | 48b76496 | Gerd Hoffmann | qemu_opt_set(opts, "path", filename);
|
2350 | 48b76496 | Gerd Hoffmann | return opts;
|
2351 | 48b76496 | Gerd Hoffmann | } |
2352 | 191bc01b | Gerd Hoffmann | |
2353 | aeb2c47a | Gerd Hoffmann | fail:
|
2354 | aeb2c47a | Gerd Hoffmann | fprintf(stderr, "%s: fail on \"%s\"\n", __FUNCTION__, filename);
|
2355 | 191bc01b | Gerd Hoffmann | qemu_opts_del(opts); |
2356 | 191bc01b | Gerd Hoffmann | return NULL; |
2357 | 191bc01b | Gerd Hoffmann | } |
2358 | 191bc01b | Gerd Hoffmann | |
2359 | 191bc01b | Gerd Hoffmann | static const struct { |
2360 | 191bc01b | Gerd Hoffmann | const char *name; |
2361 | 191bc01b | Gerd Hoffmann | CharDriverState *(*open)(QemuOpts *opts); |
2362 | 191bc01b | Gerd Hoffmann | } backend_table[] = { |
2363 | 191bc01b | Gerd Hoffmann | { .name = "null", .open = qemu_chr_open_null },
|
2364 | aeb2c47a | Gerd Hoffmann | { .name = "socket", .open = qemu_chr_open_socket },
|
2365 | 7e1b35b4 | Gerd Hoffmann | { .name = "udp", .open = qemu_chr_open_udp },
|
2366 | f0457e8d | Gerd Hoffmann | { .name = "msmouse", .open = qemu_chr_open_msmouse },
|
2367 | 6ea314d9 | Gerd Hoffmann | { .name = "vc", .open = text_console_init },
|
2368 | 7d31544f | Gerd Hoffmann | #ifdef _WIN32
|
2369 | 7d31544f | Gerd Hoffmann | { .name = "file", .open = qemu_chr_open_win_file_out },
|
2370 | 7d31544f | Gerd Hoffmann | { .name = "pipe", .open = qemu_chr_open_win_pipe },
|
2371 | d6c983cd | Gerd Hoffmann | { .name = "console", .open = qemu_chr_open_win_con },
|
2372 | 48b76496 | Gerd Hoffmann | { .name = "serial", .open = qemu_chr_open_win },
|
2373 | 7d31544f | Gerd Hoffmann | #else
|
2374 | 7d31544f | Gerd Hoffmann | { .name = "file", .open = qemu_chr_open_file_out },
|
2375 | 7d31544f | Gerd Hoffmann | { .name = "pipe", .open = qemu_chr_open_pipe },
|
2376 | 4490dadf | Gerd Hoffmann | { .name = "pty", .open = qemu_chr_open_pty },
|
2377 | 3c17affb | Gerd Hoffmann | { .name = "stdio", .open = qemu_chr_open_stdio },
|
2378 | 7d31544f | Gerd Hoffmann | #endif
|
2379 | dc1c21e6 | Gerd Hoffmann | #ifdef CONFIG_BRLAPI
|
2380 | dc1c21e6 | Gerd Hoffmann | { .name = "braille", .open = chr_baum_init },
|
2381 | dc1c21e6 | Gerd Hoffmann | #endif
|
2382 | 48b76496 | Gerd Hoffmann | #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
|
2383 | 48b76496 | Gerd Hoffmann | || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) |
2384 | 48b76496 | Gerd Hoffmann | { .name = "tty", .open = qemu_chr_open_tty },
|
2385 | 48b76496 | Gerd Hoffmann | #endif
|
2386 | 48b76496 | Gerd Hoffmann | #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
|
2387 | 48b76496 | Gerd Hoffmann | { .name = "parport", .open = qemu_chr_open_pp },
|
2388 | 48b76496 | Gerd Hoffmann | #endif
|
2389 | 191bc01b | Gerd Hoffmann | }; |
2390 | 191bc01b | Gerd Hoffmann | |
2391 | 191bc01b | Gerd Hoffmann | CharDriverState *qemu_chr_open_opts(QemuOpts *opts, |
2392 | 191bc01b | Gerd Hoffmann | void (*init)(struct CharDriverState *s)) |
2393 | 191bc01b | Gerd Hoffmann | { |
2394 | 191bc01b | Gerd Hoffmann | CharDriverState *chr; |
2395 | 191bc01b | Gerd Hoffmann | int i;
|
2396 | 191bc01b | Gerd Hoffmann | |
2397 | 191bc01b | Gerd Hoffmann | if (qemu_opts_id(opts) == NULL) { |
2398 | 191bc01b | Gerd Hoffmann | fprintf(stderr, "chardev: no id specified\n");
|
2399 | 191bc01b | Gerd Hoffmann | return NULL; |
2400 | 191bc01b | Gerd Hoffmann | } |
2401 | 191bc01b | Gerd Hoffmann | |
2402 | 191bc01b | Gerd Hoffmann | for (i = 0; i < ARRAY_SIZE(backend_table); i++) { |
2403 | 191bc01b | Gerd Hoffmann | if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0) |
2404 | 191bc01b | Gerd Hoffmann | break;
|
2405 | 191bc01b | Gerd Hoffmann | } |
2406 | 191bc01b | Gerd Hoffmann | if (i == ARRAY_SIZE(backend_table)) {
|
2407 | 191bc01b | Gerd Hoffmann | fprintf(stderr, "chardev: backend \"%s\" not found\n",
|
2408 | 191bc01b | Gerd Hoffmann | qemu_opt_get(opts, "backend"));
|
2409 | 191bc01b | Gerd Hoffmann | return NULL; |
2410 | 191bc01b | Gerd Hoffmann | } |
2411 | 191bc01b | Gerd Hoffmann | |
2412 | 191bc01b | Gerd Hoffmann | chr = backend_table[i].open(opts); |
2413 | 191bc01b | Gerd Hoffmann | if (!chr) {
|
2414 | 191bc01b | Gerd Hoffmann | fprintf(stderr, "chardev: opening backend \"%s\" failed\n",
|
2415 | 191bc01b | Gerd Hoffmann | qemu_opt_get(opts, "backend"));
|
2416 | 191bc01b | Gerd Hoffmann | return NULL; |
2417 | 191bc01b | Gerd Hoffmann | } |
2418 | 191bc01b | Gerd Hoffmann | |
2419 | 191bc01b | Gerd Hoffmann | if (!chr->filename)
|
2420 | 191bc01b | Gerd Hoffmann | chr->filename = qemu_strdup(qemu_opt_get(opts, "backend"));
|
2421 | 191bc01b | Gerd Hoffmann | chr->init = init; |
2422 | 72cf2d4f | Blue Swirl | QTAILQ_INSERT_TAIL(&chardevs, chr, next); |
2423 | 7591c5c1 | Gerd Hoffmann | |
2424 | 7591c5c1 | Gerd Hoffmann | if (qemu_opt_get_bool(opts, "mux", 0)) { |
2425 | 7591c5c1 | Gerd Hoffmann | CharDriverState *base = chr; |
2426 | 7591c5c1 | Gerd Hoffmann | int len = strlen(qemu_opts_id(opts)) + 6; |
2427 | 7591c5c1 | Gerd Hoffmann | base->label = qemu_malloc(len); |
2428 | 7591c5c1 | Gerd Hoffmann | snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
|
2429 | 7591c5c1 | Gerd Hoffmann | chr = qemu_chr_open_mux(base); |
2430 | 7591c5c1 | Gerd Hoffmann | chr->filename = base->filename; |
2431 | 72cf2d4f | Blue Swirl | QTAILQ_INSERT_TAIL(&chardevs, chr, next); |
2432 | 7591c5c1 | Gerd Hoffmann | } |
2433 | 7591c5c1 | Gerd Hoffmann | chr->label = qemu_strdup(qemu_opts_id(opts)); |
2434 | 191bc01b | Gerd Hoffmann | return chr;
|
2435 | 191bc01b | Gerd Hoffmann | } |
2436 | 191bc01b | Gerd Hoffmann | |
2437 | ceecf1d1 | aurel32 | CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s)) |
2438 | 6f97dba0 | aliguori | { |
2439 | 6f97dba0 | aliguori | const char *p; |
2440 | 6f97dba0 | aliguori | CharDriverState *chr; |
2441 | 191bc01b | Gerd Hoffmann | QemuOpts *opts; |
2442 | 191bc01b | Gerd Hoffmann | |
2443 | c845f401 | Gerd Hoffmann | if (strstart(filename, "chardev:", &p)) { |
2444 | c845f401 | Gerd Hoffmann | return qemu_chr_find(p);
|
2445 | c845f401 | Gerd Hoffmann | } |
2446 | c845f401 | Gerd Hoffmann | |
2447 | 191bc01b | Gerd Hoffmann | opts = qemu_chr_parse_compat(label, filename); |
2448 | 7e1b35b4 | Gerd Hoffmann | if (!opts)
|
2449 | 7e1b35b4 | Gerd Hoffmann | return NULL; |
2450 | 6f97dba0 | aliguori | |
2451 | 7e1b35b4 | Gerd Hoffmann | chr = qemu_chr_open_opts(opts, init); |
2452 | 7e1b35b4 | Gerd Hoffmann | if (chr && qemu_opt_get_bool(opts, "mux", 0)) { |
2453 | 7e1b35b4 | Gerd Hoffmann | monitor_init(chr, MONITOR_USE_READLINE); |
2454 | 6f97dba0 | aliguori | } |
2455 | 6f97dba0 | aliguori | return chr;
|
2456 | 6f97dba0 | aliguori | } |
2457 | 6f97dba0 | aliguori | |
2458 | 6f97dba0 | aliguori | void qemu_chr_close(CharDriverState *chr)
|
2459 | 6f97dba0 | aliguori | { |
2460 | 72cf2d4f | Blue Swirl | QTAILQ_REMOVE(&chardevs, chr, next); |
2461 | 6f97dba0 | aliguori | if (chr->chr_close)
|
2462 | 6f97dba0 | aliguori | chr->chr_close(chr); |
2463 | 6f97dba0 | aliguori | qemu_free(chr->filename); |
2464 | 6f97dba0 | aliguori | qemu_free(chr->label); |
2465 | 6f97dba0 | aliguori | qemu_free(chr); |
2466 | 6f97dba0 | aliguori | } |
2467 | 6f97dba0 | aliguori | |
2468 | 376253ec | aliguori | void qemu_chr_info(Monitor *mon)
|
2469 | 6f97dba0 | aliguori | { |
2470 | 6f97dba0 | aliguori | CharDriverState *chr; |
2471 | 6f97dba0 | aliguori | |
2472 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(chr, &chardevs, next) { |
2473 | 376253ec | aliguori | monitor_printf(mon, "%s: filename=%s\n", chr->label, chr->filename);
|
2474 | 6f97dba0 | aliguori | } |
2475 | 6f97dba0 | aliguori | } |
2476 | c845f401 | Gerd Hoffmann | |
2477 | c845f401 | Gerd Hoffmann | CharDriverState *qemu_chr_find(const char *name) |
2478 | c845f401 | Gerd Hoffmann | { |
2479 | c845f401 | Gerd Hoffmann | CharDriverState *chr; |
2480 | c845f401 | Gerd Hoffmann | |
2481 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(chr, &chardevs, next) { |
2482 | c845f401 | Gerd Hoffmann | if (strcmp(chr->label, name) != 0) |
2483 | c845f401 | Gerd Hoffmann | continue;
|
2484 | c845f401 | Gerd Hoffmann | return chr;
|
2485 | c845f401 | Gerd Hoffmann | } |
2486 | c845f401 | Gerd Hoffmann | return NULL; |
2487 | c845f401 | Gerd Hoffmann | } |