~ubuntu-branches/ubuntu/lucid/qemu-kvm/lucid

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