qemu-char.c 93 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
 * QEMU System Emulator
 *
 * Copyright (c) 2003-2008 Fabrice Bellard
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "qemu-common.h"
25
#include "monitor/monitor.h"
26
#include "ui/console.h"
27
#include "sysemu/sysemu.h"
28
#include "qemu/timer.h"
29
#include "char/char.h"
aurel32's avatar
aurel32 committed
30
#include "hw/usb.h"
Luiz Capitulino's avatar
Luiz Capitulino committed
31
#include "qmp-commands.h"
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <sys/time.h>
#include <zlib.h>

#ifndef _WIN32
#include <sys/times.h>
#include <sys/wait.h>
#include <termios.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
blueswir1's avatar
blueswir1 committed
46
#include <sys/resource.h>
47
48
#include <sys/socket.h>
#include <netinet/in.h>
blueswir1's avatar
blueswir1 committed
49
50
#include <net/if.h>
#include <arpa/inet.h>
51
52
53
#include <dirent.h>
#include <netdb.h>
#include <sys/select.h>
Juan Quintela's avatar
Juan Quintela committed
54
#ifdef CONFIG_BSD
55
#include <sys/stat.h>
Aurelien Jarno's avatar
Aurelien Jarno committed
56
57
#if defined(__GLIBC__)
#include <pty.h>
58
59
60
61
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
#include <libutil.h>
#else
#include <util.h>
Aurelien Jarno's avatar
Aurelien Jarno committed
62
#endif
63
64
65
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
#include <dev/ppbus/ppi.h>
#include <dev/ppbus/ppbconf.h>
66
67
68
#elif defined(__DragonFly__)
#include <dev/misc/ppi/ppi.h>
#include <bus/ppbus/ppbconf.h>
69
#endif
70
#else
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#ifdef __linux__
#include <pty.h>

#include <linux/ppdev.h>
#include <linux/parport.h>
#endif
#ifdef __sun__
#include <sys/stat.h>
#include <sys/ethernet.h>
#include <sys/sockio.h>
#include <netinet/arp.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h> // must come after ip.h
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <net/if.h>
#include <syslog.h>
#include <stropts.h>
#endif
#endif
#endif

95
#include "qemu/sockets.h"
Alon Levy's avatar
Alon Levy committed
96
#include "ui/qemu-spice.h"
97

98
99
#define READ_BUF_LEN 4096

100
101
102
/***********************************************************/
/* character device */

Blue Swirl's avatar
Blue Swirl committed
103
104
static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
    QTAILQ_HEAD_INITIALIZER(chardevs);
105

106
void qemu_chr_be_event(CharDriverState *s, int event)
107
{
108
109
110
111
112
113
114
115
116
117
    /* Keep track if the char device is open */
    switch (event) {
        case CHR_EVENT_OPENED:
            s->opened = 1;
            break;
        case CHR_EVENT_CLOSED:
            s->opened = 0;
            break;
    }

118
119
120
121
122
    if (!s->chr_event)
        return;
    s->chr_event(s->handler_opaque, event);
}

123
static gboolean qemu_chr_generic_open_bh(gpointer opaque)
124
125
{
    CharDriverState *s = opaque;
126
    qemu_chr_be_event(s, CHR_EVENT_OPENED);
127
128
    s->idle_tag = 0;
    return FALSE;
129
130
}

131
void qemu_chr_generic_open(CharDriverState *s)
132
{
133
134
    if (s->idle_tag == 0) {
        s->idle_tag = g_idle_add(qemu_chr_generic_open_bh, s);
135
136
137
    }
}

138
int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
139
140
141
142
{
    return s->chr_write(s, buf, len);
}

143
int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
144
145
146
147
148
149
{
    if (!s->chr_ioctl)
        return -ENOTSUP;
    return s->chr_ioctl(s, cmd, arg);
}

150
int qemu_chr_be_can_write(CharDriverState *s)
151
152
153
154
155
156
{
    if (!s->chr_can_read)
        return 0;
    return s->chr_can_read(s->handler_opaque);
}

157
void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
158
{
159
160
161
    if (s->chr_read) {
        s->chr_read(s->handler_opaque, buf, len);
    }
162
163
}

164
int qemu_chr_fe_get_msgfd(CharDriverState *s)
165
166
167
168
{
    return s->get_msgfd ? s->get_msgfd(s) : -1;
}

169
170
171
172
173
int qemu_chr_add_client(CharDriverState *s, int fd)
{
    return s->chr_add_client ? s->chr_add_client(s, fd) : -1;
}

174
175
176
177
void qemu_chr_accept_input(CharDriverState *s)
{
    if (s->chr_accept_input)
        s->chr_accept_input(s);
178
    qemu_notify_event();
179
180
}

181
void qemu_chr_fe_printf(CharDriverState *s, const char *fmt, ...)
182
{
183
    char buf[READ_BUF_LEN];
184
185
186
    va_list ap;
    va_start(ap, fmt);
    vsnprintf(buf, sizeof(buf), fmt, ap);
187
    qemu_chr_fe_write(s, (uint8_t *)buf, strlen(buf));
188
189
190
191
    va_end(ap);
}

void qemu_chr_add_handlers(CharDriverState *s,
192
                           IOCanReadHandler *fd_can_read,
193
194
195
196
                           IOReadHandler *fd_read,
                           IOEventHandler *fd_event,
                           void *opaque)
{
197
    if (!opaque && !fd_can_read && !fd_read && !fd_event) {
198
        /* chr driver being released. */
199
        ++s->avail_connections;
200
    }
201
202
203
204
205
206
    s->chr_can_read = fd_can_read;
    s->chr_read = fd_read;
    s->chr_event = fd_event;
    s->handler_opaque = opaque;
    if (s->chr_update_read_handler)
        s->chr_update_read_handler(s);
207
208
209
210
211
212

    /* We're connecting to an already opened device, so let's make sure we
       also get the open event */
    if (s->opened) {
        qemu_chr_generic_open(s);
    }
213
214
215
216
217
218
219
}

static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
{
    return len;
}

220
static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
221
222
223
{
    CharDriverState *chr;

224
    chr = g_malloc0(sizeof(CharDriverState));
225
    chr->chr_write = null_chr_write;
226
    return chr;
227
228
229
230
231
232
233
}

/* MUX driver for serial I/O splitting */
#define MAX_MUX 4
#define MUX_BUFFER_SIZE 32	/* Must be a power of 2.  */
#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
typedef struct {
234
    IOCanReadHandler *chr_can_read[MAX_MUX];
235
236
237
238
    IOReadHandler *chr_read[MAX_MUX];
    IOEventHandler *chr_event[MAX_MUX];
    void *ext_opaque[MAX_MUX];
    CharDriverState *drv;
239
    int focus;
240
241
242
    int mux_cnt;
    int term_got_escape;
    int max_size;
243
244
245
246
247
248
    /* Intermediate input buffer allows to catch escape sequences even if the
       currently active device is not accepting any input - but only until it
       is full as well. */
    unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
    int prod[MAX_MUX];
    int cons[MAX_MUX];
Jan Kiszka's avatar
Jan Kiszka committed
249
    int timestamps;
250
    int linestart;
Jan Kiszka's avatar
Jan Kiszka committed
251
    int64_t timestamps_start;
252
253
254
255
256
257
258
} MuxDriver;


static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
{
    MuxDriver *d = chr->opaque;
    int ret;
Jan Kiszka's avatar
Jan Kiszka committed
259
    if (!d->timestamps) {
260
261
262
263
264
        ret = d->drv->chr_write(d->drv, buf, len);
    } else {
        int i;

        ret = 0;
265
266
        for (i = 0; i < len; i++) {
            if (d->linestart) {
267
268
269
270
                char buf1[64];
                int64_t ti;
                int secs;

271
                ti = qemu_get_clock_ms(rt_clock);
Jan Kiszka's avatar
Jan Kiszka committed
272
273
274
                if (d->timestamps_start == -1)
                    d->timestamps_start = ti;
                ti -= d->timestamps_start;
275
                secs = ti / 1000;
276
277
278
279
280
                snprintf(buf1, sizeof(buf1),
                         "[%02d:%02d:%02d.%03d] ",
                         secs / 3600,
                         (secs / 60) % 60,
                         secs % 60,
281
                         (int)(ti % 1000));
282
                d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
283
284
285
286
287
                d->linestart = 0;
            }
            ret += d->drv->chr_write(d->drv, buf+i, 1);
            if (buf[i] == '\n') {
                d->linestart = 1;
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
            }
        }
    }
    return ret;
}

static const char * const mux_help[] = {
    "% h    print this help\n\r",
    "% x    exit emulator\n\r",
    "% s    save disk data back to file (if -snapshot)\n\r",
    "% t    toggle console timestamps\n\r"
    "% b    send break (magic sysrq)\n\r",
    "% c    switch between console and monitor\n\r",
    "% %  sends %\n\r",
    NULL
};

int term_escape_char = 0x01; /* ctrl-a is used for escape */
static void mux_print_help(CharDriverState *chr)
{
    int i, j;
    char ebuf[15] = "Escape-Char";
    char cbuf[50] = "\n\r";

    if (term_escape_char > 0 && term_escape_char < 26) {
        snprintf(cbuf, sizeof(cbuf), "\n\r");
        snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
    } else {
        snprintf(cbuf, sizeof(cbuf),
                 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
                 term_escape_char);
    }
    chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
    for (i = 0; mux_help[i] != NULL; i++) {
        for (j=0; mux_help[i][j] != '\0'; j++) {
            if (mux_help[i][j] == '%')
                chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
            else
                chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
        }
    }
}

331
332
333
334
335
336
static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
{
    if (d->chr_event[mux_nr])
        d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
}

337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
{
    if (d->term_got_escape) {
        d->term_got_escape = 0;
        if (ch == term_escape_char)
            goto send_char;
        switch(ch) {
        case '?':
        case 'h':
            mux_print_help(chr);
            break;
        case 'x':
            {
                 const char *term =  "QEMU: Terminated\n\r";
                 chr->chr_write(chr,(uint8_t *)term,strlen(term));
                 exit(0);
                 break;
            }
        case 's':
356
            bdrv_commit_all();
357
358
            break;
        case 'b':
359
            qemu_chr_be_event(chr, CHR_EVENT_BREAK);
360
361
362
            break;
        case 'c':
            /* Switch to the next registered device */
363
364
365
366
367
            mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
            d->focus++;
            if (d->focus >= d->mux_cnt)
                d->focus = 0;
            mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
368
            break;
Jan Kiszka's avatar
Jan Kiszka committed
369
370
371
        case 't':
            d->timestamps = !d->timestamps;
            d->timestamps_start = -1;
372
            d->linestart = 0;
Jan Kiszka's avatar
Jan Kiszka committed
373
            break;
374
375
376
377
378
379
380
381
382
383
384
385
386
        }
    } else if (ch == term_escape_char) {
        d->term_got_escape = 1;
    } else {
    send_char:
        return 1;
    }
    return 0;
}

static void mux_chr_accept_input(CharDriverState *chr)
{
    MuxDriver *d = chr->opaque;
387
    int m = d->focus;
388

389
    while (d->prod[m] != d->cons[m] &&
390
391
392
           d->chr_can_read[m] &&
           d->chr_can_read[m](d->ext_opaque[m])) {
        d->chr_read[m](d->ext_opaque[m],
393
                       &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
394
395
396
397
398
399
400
    }
}

static int mux_chr_can_read(void *opaque)
{
    CharDriverState *chr = opaque;
    MuxDriver *d = chr->opaque;
401
    int m = d->focus;
402

403
    if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
404
        return 1;
405
406
    if (d->chr_can_read[m])
        return d->chr_can_read[m](d->ext_opaque[m]);
407
408
409
410
411
412
413
    return 0;
}

static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
{
    CharDriverState *chr = opaque;
    MuxDriver *d = chr->opaque;
414
    int m = d->focus;
415
416
417
418
419
420
    int i;

    mux_chr_accept_input (opaque);

    for(i = 0; i < size; i++)
        if (mux_proc_byte(chr, d, buf[i])) {
421
            if (d->prod[m] == d->cons[m] &&
422
423
424
425
                d->chr_can_read[m] &&
                d->chr_can_read[m](d->ext_opaque[m]))
                d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
            else
426
                d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
427
428
429
430
431
432
433
434
435
436
437
        }
}

static void mux_chr_event(void *opaque, int event)
{
    CharDriverState *chr = opaque;
    MuxDriver *d = chr->opaque;
    int i;

    /* Send the event to all registered listeners */
    for (i = 0; i < d->mux_cnt; i++)
438
        mux_chr_send_event(d, i, event);
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
}

static void mux_chr_update_read_handler(CharDriverState *chr)
{
    MuxDriver *d = chr->opaque;

    if (d->mux_cnt >= MAX_MUX) {
        fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
        return;
    }
    d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
    d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
    d->chr_read[d->mux_cnt] = chr->chr_read;
    d->chr_event[d->mux_cnt] = chr->chr_event;
    /* Fix up the real driver with mux routines */
    if (d->mux_cnt == 0) {
        qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
                              mux_chr_event, chr);
    }
458
459
    if (d->focus != -1) {
        mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Gerd Hoffmann's avatar
Gerd Hoffmann committed
460
    }
461
    d->focus = d->mux_cnt;
462
    d->mux_cnt++;
463
    mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
464
465
466
467
468
469
470
}

static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
{
    CharDriverState *chr;
    MuxDriver *d;

471
472
    chr = g_malloc0(sizeof(CharDriverState));
    d = g_malloc0(sizeof(MuxDriver));
473
474
475

    chr->opaque = d;
    d->drv = drv;
476
    d->focus = -1;
477
478
479
    chr->chr_write = mux_chr_write;
    chr->chr_update_read_handler = mux_chr_update_read_handler;
    chr->chr_accept_input = mux_chr_accept_input;
480
481
482
    /* Frontend guest-open / -close notification is not support with muxes */
    chr->chr_guest_open = NULL;
    chr->chr_guest_close = NULL;
483
484
485
486

    /* Muxes are always open on creation */
    qemu_chr_generic_open(chr);

487
488
489
490
491
    return chr;
}


#ifdef _WIN32
492
int send_all(int fd, const void *buf, int len1)
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
{
    int ret, len;

    len = len1;
    while (len > 0) {
        ret = send(fd, buf, len, 0);
        if (ret < 0) {
            errno = WSAGetLastError();
            if (errno != WSAEWOULDBLOCK) {
                return -1;
            }
        } else if (ret == 0) {
            break;
        } else {
            buf += ret;
            len -= ret;
        }
    }
    return len1 - len;
}

#else

516
int send_all(int fd, const void *_buf, int len1)
517
518
{
    int ret, len;
519
    const uint8_t *buf = _buf;
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537

    len = len1;
    while (len > 0) {
        ret = write(fd, buf, len);
        if (ret < 0) {
            if (errno != EINTR && errno != EAGAIN)
                return -1;
        } else if (ret == 0) {
            break;
        } else {
            buf += ret;
            len -= ret;
        }
    }
    return len1 - len;
}
#endif /* !_WIN32 */

538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
typedef struct IOWatchPoll
{
    GSource *src;
    int max_size;

    IOCanReadHandler *fd_can_read;
    void *opaque;

    QTAILQ_ENTRY(IOWatchPoll) node;
} IOWatchPoll;

static QTAILQ_HEAD(, IOWatchPoll) io_watch_poll_list =
    QTAILQ_HEAD_INITIALIZER(io_watch_poll_list);

static IOWatchPoll *io_watch_poll_from_source(GSource *source)
{
    IOWatchPoll *i;

    QTAILQ_FOREACH(i, &io_watch_poll_list, node) {
        if (i->src == source) {
            return i;
        }
    }

    return NULL;
}

static gboolean io_watch_poll_prepare(GSource *source, gint *timeout_)
{
    IOWatchPoll *iwp = io_watch_poll_from_source(source);

    iwp->max_size = iwp->fd_can_read(iwp->opaque);
    if (iwp->max_size == 0) {
        return FALSE;
    }

    return g_io_watch_funcs.prepare(source, timeout_);
}

static gboolean io_watch_poll_check(GSource *source)
{
    IOWatchPoll *iwp = io_watch_poll_from_source(source);

    if (iwp->max_size == 0) {
        return FALSE;
    }

    return g_io_watch_funcs.check(source);
}

static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
                                       gpointer user_data)
{
    return g_io_watch_funcs.dispatch(source, callback, user_data);
}

static void io_watch_poll_finalize(GSource *source)
{
    IOWatchPoll *iwp = io_watch_poll_from_source(source);
    QTAILQ_REMOVE(&io_watch_poll_list, iwp, node);
    g_io_watch_funcs.finalize(source);
}

static GSourceFuncs io_watch_poll_funcs = {
    .prepare = io_watch_poll_prepare,
    .check = io_watch_poll_check,
    .dispatch = io_watch_poll_dispatch,
    .finalize = io_watch_poll_finalize,
};

/* Can only be used for read */
static guint io_add_watch_poll(GIOChannel *channel,
                               IOCanReadHandler *fd_can_read,
                               GIOFunc fd_read,
                               gpointer user_data)
{
    IOWatchPoll *iwp;
    GSource *src;
    guint tag;

    src = g_io_create_watch(channel, G_IO_IN | G_IO_ERR | G_IO_HUP);
    g_source_set_funcs(src, &io_watch_poll_funcs);
    g_source_set_callback(src, (GSourceFunc)fd_read, user_data, NULL);
    tag = g_source_attach(src, NULL);
    g_source_unref(src);

    iwp = g_malloc0(sizeof(*iwp));
    iwp->src = src;
    iwp->max_size = 0;
    iwp->fd_can_read = fd_can_read;
    iwp->opaque = user_data;

    QTAILQ_INSERT_HEAD(&io_watch_poll_list, iwp, node);

    return tag;
}

Blue Swirl's avatar
Blue Swirl committed
635
#ifndef _WIN32
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
static GIOChannel *io_channel_from_fd(int fd)
{
    GIOChannel *chan;

    if (fd == -1) {
        return NULL;
    }

    chan = g_io_channel_unix_new(fd);

    g_io_channel_set_encoding(chan, NULL, NULL);
    g_io_channel_set_buffered(chan, FALSE);

    return chan;
}
Blue Swirl's avatar
Blue Swirl committed
651
#endif
652

653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
static GIOChannel *io_channel_from_socket(int fd)
{
    GIOChannel *chan;

    if (fd == -1) {
        return NULL;
    }

#ifdef _WIN32
    chan = g_io_channel_win32_new_socket(fd);
#else
    chan = g_io_channel_unix_new(fd);
#endif

    g_io_channel_set_encoding(chan, NULL, NULL);
    g_io_channel_set_buffered(chan, FALSE);

    return chan;
}

673
674
675
676
677
678
679
680
681
682
683
684
static int io_channel_send_all(GIOChannel *fd, const void *_buf, int len1)
{
    GIOStatus status;
    gsize bytes_written;
    int len;
    const uint8_t *buf = _buf;

    len = len1;
    while (len > 0) {
        status = g_io_channel_write_chars(fd, (const gchar *)buf, len,
                                          &bytes_written, NULL);
        if (status != G_IO_STATUS_NORMAL) {
Anthony Liguori's avatar
Anthony Liguori committed
685
686
687
688
689
            if (status == G_IO_STATUS_AGAIN) {
                errno = EAGAIN;
                return -1;
            } else {
                errno = EINVAL;
690
691
692
693
694
695
696
697
698
699
700
701
                return -1;
            }
        } else if (status == G_IO_STATUS_EOF) {
            break;
        } else {
            buf += bytes_written;
            len -= bytes_written;
        }
    }
    return len1 - len;
}

Blue Swirl's avatar
Blue Swirl committed
702
703
#ifndef _WIN32

704
705
706
707
typedef struct FDCharDriver {
    CharDriverState *chr;
    GIOChannel *fd_in, *fd_out;
    guint fd_in_tag;
708
    int max_size;
709
    QTAILQ_ENTRY(FDCharDriver) node;
710
711
712
713
714
} FDCharDriver;

static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
{
    FDCharDriver *s = chr->opaque;
715
716
    
    return io_channel_send_all(s->fd_out, buf, len);
717
718
}

719
static gboolean fd_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
720
721
722
{
    CharDriverState *chr = opaque;
    FDCharDriver *s = chr->opaque;
723
    int len;
724
    uint8_t buf[READ_BUF_LEN];
725
726
    GIOStatus status;
    gsize bytes_read;
727
728

    len = sizeof(buf);
729
    if (len > s->max_size) {
730
        len = s->max_size;
731
732
733
734
735
736
737
738
    }
    if (len == 0) {
        return FALSE;
    }

    status = g_io_channel_read_chars(chan, (gchar *)buf,
                                     len, &bytes_read, NULL);
    if (status == G_IO_STATUS_EOF) {
739
        qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
740
        return FALSE;
741
    }
742
743
    if (status == G_IO_STATUS_NORMAL) {
        qemu_chr_be_write(chr, buf, bytes_read);
744
    }
745
746
747
748
749
750
751
752
753
754
755

    return TRUE;
}

static int fd_chr_read_poll(void *opaque)
{
    CharDriverState *chr = opaque;
    FDCharDriver *s = chr->opaque;

    s->max_size = qemu_chr_be_can_write(chr);
    return s->max_size;
756
757
}

Anthony Liguori's avatar
Anthony Liguori committed
758
759
760
761
762
763
static GSource *fd_chr_add_watch(CharDriverState *chr, GIOCondition cond)
{
    FDCharDriver *s = chr->opaque;
    return g_io_create_watch(s->fd_out, cond);
}

764
765
766
767
static void fd_chr_update_read_handler(CharDriverState *chr)
{
    FDCharDriver *s = chr->opaque;

768
769
770
771
772
773
    if (s->fd_in_tag) {
        g_source_remove(s->fd_in_tag);
    }

    if (s->fd_in) {
        s->fd_in_tag = io_add_watch_poll(s->fd_in, fd_chr_read_poll, fd_chr_read, chr);
774
775
776
777
778
779
780
    }
}

static void fd_chr_close(struct CharDriverState *chr)
{
    FDCharDriver *s = chr->opaque;

781
782
783
784
785
786
787
788
789
790
    if (s->fd_in_tag) {
        g_source_remove(s->fd_in_tag);
        s->fd_in_tag = 0;
    }

    if (s->fd_in) {
        g_io_channel_unref(s->fd_in);
    }
    if (s->fd_out) {
        g_io_channel_unref(s->fd_out);
791
792
    }

793
    g_free(s);
794
    qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
795
796
797
798
799
800
801
802
}

/* open a character device to a unix fd */
static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
{
    CharDriverState *chr;
    FDCharDriver *s;

803
804
    chr = g_malloc0(sizeof(CharDriverState));
    s = g_malloc0(sizeof(FDCharDriver));
805
806
    s->fd_in = io_channel_from_fd(fd_in);
    s->fd_out = io_channel_from_fd(fd_out);
Anthony Liguori's avatar
Anthony Liguori committed
807
    fcntl(fd_out, F_SETFL, O_NONBLOCK);
808
    s->chr = chr;
809
    chr->opaque = s;
Anthony Liguori's avatar
Anthony Liguori committed
810
    chr->chr_add_watch = fd_chr_add_watch;
811
812
813
814
    chr->chr_write = fd_chr_write;
    chr->chr_update_read_handler = fd_chr_update_read_handler;
    chr->chr_close = fd_chr_close;

815
    qemu_chr_generic_open(chr);
816
817
818
819

    return chr;
}

820
static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
821
822
823
{
    int fd_out;

Kevin Wolf's avatar
Kevin Wolf committed
824
    TFR(fd_out = qemu_open(qemu_opt_get(opts, "path"),
825
                      O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
826
    if (fd_out < 0) {
827
        return NULL;
828
    }
829
    return qemu_chr_open_fd(-1, fd_out);
830
831
}

832
static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
833
834
835
{
    int fd_in, fd_out;
    char filename_in[256], filename_out[256];
836
837
838
839
    const char *filename = qemu_opt_get(opts, "path");

    if (filename == NULL) {
        fprintf(stderr, "chardev: pipe: no filename given\n");
840
        return NULL;
841
    }
842
843
844

    snprintf(filename_in, 256, "%s.in", filename);
    snprintf(filename_out, 256, "%s.out", filename);
Kevin Wolf's avatar
Kevin Wolf committed
845
846
    TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
    TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
847
848
849
850
851
    if (fd_in < 0 || fd_out < 0) {
	if (fd_in >= 0)
	    close(fd_in);
	if (fd_out >= 0)
	    close(fd_out);
852
        TFR(fd_in = fd_out = qemu_open(filename, O_RDWR | O_BINARY));
853
        if (fd_in < 0) {
854
            return NULL;
855
        }
856
    }
857
    return qemu_chr_open_fd(fd_in, fd_out);
858
859
860
861
862
}

/* init terminal so that we can grab keys */
static struct termios oldtty;
static int old_fd0_flags;
863
static bool stdio_allow_signal;
864
865
866
867
868
869
870

static void term_exit(void)
{
    tcsetattr (0, TCSANOW, &oldtty);
    fcntl(0, F_SETFL, old_fd0_flags);
}

871
static void qemu_chr_set_echo_stdio(CharDriverState *chr, bool echo)
872
873
874
{
    struct termios tty;

875
    tty = oldtty;
876
877
    if (!echo) {
        tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
878
                          |INLCR|IGNCR|ICRNL|IXON);
879
880
881
882
883
884
885
        tty.c_oflag |= OPOST;
        tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
        tty.c_cflag &= ~(CSIZE|PARENB);
        tty.c_cflag |= CS8;
        tty.c_cc[VMIN] = 1;
        tty.c_cc[VTIME] = 0;
    }
886
    /* if graphical mode, we allow Ctrl-C handling */
887
    if (!stdio_allow_signal)
888
889
890
891
892
893
894
895
896
897
898
        tty.c_lflag &= ~ISIG;

    tcsetattr (0, TCSANOW, &tty);
}

static void qemu_chr_close_stdio(struct CharDriverState *chr)
{
    term_exit();
    fd_chr_close(chr);
}

899
static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
900
901
902
{
    CharDriverState *chr;

903
904
905
906
    if (is_daemonized()) {
        error_report("cannot use stdio with -daemonize");
        return NULL;
    }
907
908
909
910
    old_fd0_flags = fcntl(0, F_GETFL);
    tcgetattr (0, &oldtty);
    fcntl(0, F_SETFL, O_NONBLOCK);
    atexit(term_exit);
911

912
913
    chr = qemu_chr_open_fd(0, 1);
    chr->chr_close = qemu_chr_close_stdio;
914
915
916
    chr->chr_set_echo = qemu_chr_set_echo_stdio;
    stdio_allow_signal = qemu_opt_get_bool(opts, "signal",
                                           display_type != DT_NOGRAPHIC);
917
    qemu_chr_fe_set_echo(chr, false);
918

919
    return chr;
920
921
922
923
}

#ifdef __sun__
/* Once Solaris has openpty(), this is going to be removed. */
924
925
static int openpty(int *amaster, int *aslave, char *name,
                   struct termios *termp, struct winsize *winp)
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
{
        const char *slave;
        int mfd = -1, sfd = -1;

        *amaster = *aslave = -1;

        mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
        if (mfd < 0)
                goto err;

        if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
                goto err;

        if ((slave = ptsname(mfd)) == NULL)
                goto err;

        if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
                goto err;

        if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
            (termp != NULL && tcgetattr(sfd, termp) < 0))
                goto err;

        if (amaster)
                *amaster = mfd;
        if (aslave)
                *aslave = sfd;
        if (winp)
                ioctl(sfd, TIOCSWINSZ, winp);

        return 0;

err:
        if (sfd != -1)
                close(sfd);
        close(mfd);
        return -1;
}

965
static void cfmakeraw (struct termios *termios_p)
966
967
968
969
970
971
972
973
974
975
976
977
978
979
{
        termios_p->c_iflag &=
                ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
        termios_p->c_oflag &= ~OPOST;
        termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
        termios_p->c_cflag &= ~(CSIZE|PARENB);
        termios_p->c_cflag |= CS8;

        termios_p->c_cc[VMIN] = 0;
        termios_p->c_cc[VTIME] = 0;
}
#endif

#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
Aurelien Jarno's avatar
Aurelien Jarno committed
980
981
    || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
    || defined(__GLIBC__)
982

983
984
#define HAVE_CHARDEV_TTY 1

985
typedef struct {
986
987
    GIOChannel *fd;
    guint fd_tag;
988
989
990
    int connected;
    int polling;
    int read_bytes;
991
    guint timer_tag;
992
993
994
995
996
} PtyCharDriver;

static void pty_chr_update_read_handler(CharDriverState *chr);
static void pty_chr_state(CharDriverState *chr, int connected);

997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
static gboolean pty_chr_timer(gpointer opaque)
{
    struct CharDriverState *chr = opaque;
    PtyCharDriver *s = chr->opaque;

    if (s->connected) {
        goto out;
    }
    if (s->polling) {
        /* If we arrive here without polling being cleared due
         * read returning -EIO, then we are (re-)connected */
        pty_chr_state(chr, 1);
        goto out;
    }

    /* Next poll ... */
    pty_chr_update_read_handler(chr);

out:
    return FALSE;
}

static void pty_chr_rearm_timer(CharDriverState *chr, int ms)
{
    PtyCharDriver *s = chr->opaque;

    if (s->timer_tag) {
        g_source_remove(s->timer_tag);
        s->timer_tag = 0;
    }

    if (ms == 1000) {
        s->timer_tag = g_timeout_add_seconds(1, pty_chr_timer, chr);
    } else {
        s->timer_tag = g_timeout_add(ms, pty_chr_timer, chr);
    }
}

1035
1036
1037
1038
1039
1040
1041
1042
1043
static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
{
    PtyCharDriver *s = chr->opaque;

    if (!s->connected) {
        /* guest sends data, check for (re-)connect */
        pty_chr_update_read_handler(chr);
        return 0;
    }
1044
    return io_channel_send_all(s->fd, buf, len);
1045
1046
}

Anthony Liguori's avatar
Anthony Liguori committed
1047
1048
1049
1050
1051
1052
static GSource *pty_chr_add_watch(CharDriverState *chr, GIOCondition cond)
{
    PtyCharDriver *s = chr->opaque;
    return g_io_create_watch(s->fd, cond);
}

1053
1054
1055
1056
1057
static int pty_chr_read_poll(void *opaque)
{
    CharDriverState *chr = opaque;
    PtyCharDriver *s = chr->opaque;

1058
    s->read_bytes = qemu_chr_be_can_write(chr);
1059
1060
1061
    return s->read_bytes;
}

1062
static gboolean pty_chr_read(GIOChannel *chan, GIOCondition cond, void *opaque)
1063
1064
1065
{
    CharDriverState *chr = opaque;
    PtyCharDriver *s = chr->opaque;
1066
    gsize size, len;
1067
    uint8_t buf[READ_BUF_LEN];
1068
    GIOStatus status;
1069
1070
1071
1072
1073

    len = sizeof(buf);
    if (len > s->read_bytes)
        len = s->read_bytes;
    if (len == 0)
1074
1075
1076
        return FALSE;
    status = g_io_channel_read_chars(s->fd, (gchar *)buf, len, &size, NULL);
    if (status != G_IO_STATUS_NORMAL) {
1077
        pty_chr_state(chr, 0);
1078
1079
        return FALSE;
    } else {
1080
        pty_chr_state(chr, 1);
1081
        qemu_chr_be_write(chr, buf, size);
1082
    }
1083
    return TRUE;
1084
1085
1086
1087
1088
1089
}

static void pty_chr_update_read_handler(CharDriverState *chr)
{
    PtyCharDriver *s = chr->opaque;

1090
1091
1092
1093
1094
    if (s->fd_tag) {
        g_source_remove(s->fd_tag);
    }

    s->fd_tag = io_add_watch_poll(s->fd, pty_chr_read_poll, pty_chr_read, chr);
1095
1096
1097
1098
1099
1100
1101
1102
1103
    s->polling = 1;
    /*
     * Short timeout here: just need wait long enougth that qemu makes
     * it through the poll loop once.  When reconnected we want a
     * short timeout so we notice it almost instantly.  Otherwise
     * read() gives us -EIO instantly, making pty_chr_state() reset the
     * timeout to the normal (much longer) poll interval before the
     * timer triggers.
     */
1104
    pty_chr_rearm_timer(chr, 10);
1105
1106
1107
1108
1109
1110
1111
}

static void pty_chr_state(CharDriverState *chr, int connected)
{
    PtyCharDriver *s = chr->opaque;

    if (!connected) {
1112
1113
        g_source_remove(s->fd_tag);
        s->fd_tag = 0;
1114
1115
1116
1117
1118
        s->connected = 0;
        s->polling = 0;
        /* (re-)connect poll interval for idle guests: once per second.
         * We check more frequently in case the guests sends data to
         * the virtual device linked to our pty. */
1119
        pty_chr_rearm_timer(chr, 1000);
1120
1121
    } else {
        if (!s->connected)
1122
            qemu_chr_generic_open(chr);
1123
1124
1125
1126
1127
1128
1129
1130
        s->connected = 1;
    }
}


static void pty_chr_close(struct CharDriverState *chr)
{
    PtyCharDriver *s = chr->opaque;
1131
    int fd;
1132

1133
1134
1135
1136
1137
1138
    if (s->fd_tag) {
        g_source_remove(s->fd_tag);
    }
    fd = g_io_channel_unix_get_fd(s->fd);
    g_io_channel_unref(s->fd);
    close(fd);
1139
1140
1141
    if (s->timer_tag) {
        g_source_remove(s->timer_tag);
    }
1142
    g_free(s);
1143
    qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1144
1145
}

1146
static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
1147
1148
1149
1150
{
    CharDriverState *chr;
    PtyCharDriver *s;
    struct termios tty;
1151
    const char *label;
1152
    int master_fd, slave_fd, len;
1153
#if defined(__OpenBSD__) || defined(__DragonFly__)
1154
1155
1156
1157
1158
1159
1160
    char pty_name[PATH_MAX];
#define q_ptsname(x) pty_name
#else
    char *pty_name = NULL;
#define q_ptsname(x) ptsname(x)
#endif

1161
    if (openpty(&master_fd, &slave_fd, pty_name, NULL, NULL) < 0) {
1162
        return NULL;
1163
1164
1165
    }

    /* Set raw attributes on the pty. */
1166
    tcgetattr(slave_fd, &tty);
1167
1168
1169
1170
    cfmakeraw(&tty);
    tcsetattr(slave_fd, TCSAFLUSH, &tty);
    close(slave_fd);

1171
1172
1173
    chr = g_malloc0(sizeof(CharDriverState));

    len = strlen(q_ptsname(master_fd)) + 5;
1174
    chr->filename = g_malloc(len);
1175
1176
    snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd));
    qemu_opt_set(opts, "path", q_ptsname(master_fd));
1177
1178

    label = qemu_opts_id(opts);
Gerd Hoffmann's avatar
Gerd Hoffmann committed
1179
1180
1181
1182
1183
    fprintf(stderr, "char device redirected to %s%s%s%s\n",
            q_ptsname(master_fd),
            label ? " (label " : "",
            label ? label      : "",
            label ? ")"        : "");
1184

1185
    s = g_malloc0(sizeof(PtyCharDriver));
1186
1187
1188
1189
    chr->opaque = s;
    chr->chr_write = pty_chr_write;
    chr->chr_update_read_handler = pty_chr_update_read_handler;
    chr->chr_close = pty_chr_close;
Anthony Liguori's avatar
Anthony Liguori committed
1190
    chr->chr_add_watch = pty_chr_add_watch;
1191

1192
    s->fd = io_channel_from_fd(master_fd);
1193
    s->timer_tag = 0;
1194

1195
    return chr;
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
}

static void tty_serial_init(int fd, int speed,
                            int parity, int data_bits, int stop_bits)
{
    struct termios tty;
    speed_t spd;

#if 0
    printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
           speed, parity, data_bits, stop_bits);
#endif
    tcgetattr (fd, &tty);

1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
#define check_speed(val) if (speed <= val) { spd = B##val; break; }
    speed = speed * 10 / 11;
    do {
        check_speed(50);
        check_speed(75);
        check_speed(110);
        check_speed(134);
        check_speed(150);
        check_speed(200);
        check_speed(300);
        check_speed(600);
        check_speed(1200);
        check_speed(1800);
        check_speed(2400);
        check_speed(4800);
        check_speed(9600);
        check_speed(19200);
        check_speed(38400);
        /* Non-Posix values follow. They may be unsupported on some systems. */
        check_speed(57600);
        check_speed(115200);
#ifdef B230400
        check_speed(230400);
#endif
#ifdef B460800
        check_speed(460800);
#endif
#ifdef B500000
        check_speed(500000);
#endif
#ifdef B576000
        check_speed(576000);
#endif
#ifdef B921600
        check_speed(921600);
#endif
#ifdef B1000000
        check_speed(1000000);
#endif
#ifdef B1152000
        check_speed(1152000);
#endif
#ifdef B1500000
        check_speed(1500000);
#endif
#ifdef B2000000
        check_speed(2000000);
#endif
#ifdef B2500000
        check_speed(2500000);
#endif
#ifdef B3000000
        check_speed(3000000);
#endif
#ifdef B3500000
        check_speed(3500000);
#endif
#ifdef B4000000
        check_speed(4000000);
#endif
1270
        spd = B115200;
1271
    } while (0);
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320

    cfsetispeed(&tty, spd);
    cfsetospeed(&tty, spd);

    tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
                          |INLCR|IGNCR|ICRNL|IXON);
    tty.c_oflag |= OPOST;
    tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
    tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
    switch(data_bits) {
    default:
    case 8:
        tty.c_cflag |= CS8;
        break;
    case 7:
        tty.c_cflag |= CS7;
        break;
    case 6:
        tty.c_cflag |= CS6;
        break;
    case 5:
        tty.c_cflag |= CS5;
        break;
    }
    switch(parity) {
    default:
    case 'N':
        break;
    case 'E':
        tty.c_cflag |= PARENB;
        break;
    case 'O':
        tty.c_cflag |= PARENB | PARODD;
        break;
    }
    if (stop_bits == 2)
        tty.c_cflag |= CSTOPB;

    tcsetattr (fd, TCSANOW, &tty);
}

static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
{
    FDCharDriver *s = chr->opaque;

    switch(cmd) {
    case CHR_IOCTL_SERIAL_SET_PARAMS:
        {
            QEMUSerialSetParams *ssp = arg;
1321
1322
            tty_serial_init(g_io_channel_unix_get_fd(s->fd_in),
                            ssp->speed, ssp->parity,
1323
1324
1325
1326
1327
1328
                            ssp->data_bits, ssp->stop_bits);
        }
        break;
    case CHR_IOCTL_SERIAL_SET_BREAK:
        {
            int enable = *(int *)arg;
1329
1330
1331
            if (enable) {
                tcsendbreak(g_io_channel_unix_get_fd(s->fd_in), 1);
            }
1332
1333
1334
1335
1336
1337
        }
        break;
    case CHR_IOCTL_SERIAL_GET_TIOCM:
        {
            int sarg = 0;
            int *targ = (int *)arg;
1338
            ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &sarg);
1339
            *targ = 0;
aurel32's avatar
aurel32 committed
1340
            if (sarg & TIOCM_CTS)
1341
                *targ |= CHR_TIOCM_CTS;
aurel32's avatar
aurel32 committed
1342
            if (sarg & TIOCM_CAR)
1343
                *targ |= CHR_TIOCM_CAR;
aurel32's avatar
aurel32 committed
1344
            if (sarg & TIOCM_DSR)
1345
                *targ |= CHR_TIOCM_DSR;
aurel32's avatar
aurel32 committed
1346
            if (sarg & TIOCM_RI)
1347
                *targ |= CHR_TIOCM_RI;
aurel32's avatar
aurel32 committed
1348
            if (sarg & TIOCM_DTR)
1349
                *targ |= CHR_TIOCM_DTR;
aurel32's avatar
aurel32 committed
1350
            if (sarg & TIOCM_RTS)
1351
1352
1353
1354
1355
1356
1357
                *targ |= CHR_TIOCM_RTS;
        }
        break;
    case CHR_IOCTL_SERIAL_SET_TIOCM:
        {
            int sarg = *(int *)arg;
            int targ = 0;
1358
            ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMGET, &targ);
aurel32's avatar
aurel32 committed
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
            targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
                     | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
            if (sarg & CHR_TIOCM_CTS)
                targ |= TIOCM_CTS;
            if (sarg & CHR_TIOCM_CAR)
                targ |= TIOCM_CAR;
            if (sarg & CHR_TIOCM_DSR)
                targ |= TIOCM_DSR;
            if (sarg & CHR_TIOCM_RI)
                targ |= TIOCM_RI;
            if (sarg & CHR_TIOCM_DTR)
1370
                targ |= TIOCM_DTR;
aurel32's avatar
aurel32 committed
1371
            if (sarg & CHR_TIOCM_RTS)
1372
                targ |= TIOCM_RTS;
1373
            ioctl(g_io_channel_unix_get_fd(s->fd_in), TIOCMSET, &targ);
1374
1375
1376
1377
1378
1379
1380
1381
        }
        break;
    default:
        return -ENOTSUP;
    }
    return 0;
}

1382
1383
1384
1385
1386
1387
static void qemu_chr_close_tty(CharDriverState *chr)
{
    FDCharDriver *s = chr->opaque;
    int fd = -1;

    if (s) {
1388
        fd = g_io_channel_unix_get_fd(s->fd_in);
1389
1390
1391
1392
1393
1394
1395
1396
1397
    }

    fd_chr_close(chr);

    if (fd >= 0) {
        close(fd);
    }
}

1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
static CharDriverState *qemu_chr_open_tty_fd(int fd)
{
    CharDriverState *chr;

    tty_serial_init(fd, 115200, 'N', 8, 1);
    chr = qemu_chr_open_fd(fd, fd);
    chr->chr_ioctl = tty_serial_ioctl;
    chr->chr_close = qemu_chr_close_tty;
    return chr;
}

1409
static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
1410
{
1411
    const char *filename = qemu_opt_get(opts, "path");