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