Patchwork qemu-char: inherit ptys and imporve output from -serial pty

login
register
mail settings
Submitter Lei Li
Date Dec. 20, 2012, 2:49 p.m.
Message ID <1356014948-13450-1-git-send-email-lilei@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/207675/
State New
Headers show

Comments

Lei Li - Dec. 20, 2012, 2:49 p.m.
When controlling a qemu instance from another program, it's
hard to know which serial port or monitor device is redirected
to which pty. With more than one device using "pty" a lot of
guesswork is involved.

$ ./x86_64-softmmu/qemu-system-x86_64 -serial pty -serial pty -monitor pty
char device redirected to /dev/pts/5
char device redirected to /dev/pts/6
char device redirected to /dev/pts/7

Although we can find out what everything else is connected to
by the "info chardev" with "-monitor stdio" in the command line,
It'd be very useful to be able to have qemu inherit pseudo-tty
file descriptors so they could just be specified on the command
line like:

$ ./x86_64-softmmu/qemu-system-x86_64 -serial pty -serial pty -monitor pty
char device compat_monitor0 redirected to /dev/pts/5
char device serial0 redirected to /dev/pts/6
char device serial1 redirected to /dev/pts/7

Referred link: https://bugs.launchpad.net/qemu/+bug/938552

Reported-by: Craig Ringer <ringerc@gmail.com>
Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
---
 qemu-char.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
Lei Li - Dec. 20, 2012, 2:58 p.m.
Sorry, s/imporve/improve...

On 12/20/2012 10:49 PM, Lei Li wrote:
> When controlling a qemu instance from another program, it's
> hard to know which serial port or monitor device is redirected
> to which pty. With more than one device using "pty" a lot of
> guesswork is involved.
>
> $ ./x86_64-softmmu/qemu-system-x86_64 -serial pty -serial pty -monitor pty
> char device redirected to /dev/pts/5
> char device redirected to /dev/pts/6
> char device redirected to /dev/pts/7
>
> Although we can find out what everything else is connected to
> by the "info chardev" with "-monitor stdio" in the command line,
> It'd be very useful to be able to have qemu inherit pseudo-tty
> file descriptors so they could just be specified on the command
> line like:
>
> $ ./x86_64-softmmu/qemu-system-x86_64 -serial pty -serial pty -monitor pty
> char device compat_monitor0 redirected to /dev/pts/5
> char device serial0 redirected to /dev/pts/6
> char device serial1 redirected to /dev/pts/7
>
> Referred link: https://bugs.launchpad.net/qemu/+bug/938552
>
> Reported-by: Craig Ringer <ringerc@gmail.com>
> Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
> ---
>   qemu-char.c |    4 +++-
>   1 files changed, 3 insertions(+), 1 deletions(-)
>
> diff --git a/qemu-char.c b/qemu-char.c
> index 242b799..2b0f5f4 100644
> --- a/qemu-char.c
> +++ b/qemu-char.c
> @@ -981,6 +981,7 @@ static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
>       CharDriverState *chr;
>       PtyCharDriver *s;
>       struct termios tty;
> +    char *label;
>       int master_fd, slave_fd, len;
>   #if defined(__OpenBSD__) || defined(__DragonFly__)
>       char pty_name[PATH_MAX];
> @@ -1006,7 +1007,8 @@ static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
>       chr->filename = g_malloc(len);
>       snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd));
>       qemu_opt_set(opts, "path", q_ptsname(master_fd));
> -    fprintf(stderr, "char device redirected to %s\n", q_ptsname(master_fd));
> +    label = g_strdup(qemu_opts_id(opts));
> +    fprintf(stderr, "char device %s redirected to %s\n", label, q_ptsname(master_fd));
>
>       s = g_malloc0(sizeof(PtyCharDriver));
>       chr->opaque = s;
Markus Armbruster - Dec. 20, 2012, 3:19 p.m.
Lei Li <lilei@linux.vnet.ibm.com> writes:

> When controlling a qemu instance from another program, it's
> hard to know which serial port or monitor device is redirected
> to which pty. With more than one device using "pty" a lot of
> guesswork is involved.
>
> $ ./x86_64-softmmu/qemu-system-x86_64 -serial pty -serial pty -monitor pty
> char device redirected to /dev/pts/5
> char device redirected to /dev/pts/6
> char device redirected to /dev/pts/7
>
> Although we can find out what everything else is connected to
> by the "info chardev" with "-monitor stdio" in the command line,
> It'd be very useful to be able to have qemu inherit pseudo-tty
> file descriptors so they could just be specified on the command
> line like:
>
> $ ./x86_64-softmmu/qemu-system-x86_64 -serial pty -serial pty -monitor pty
> char device compat_monitor0 redirected to /dev/pts/5
> char device serial0 redirected to /dev/pts/6
> char device serial1 redirected to /dev/pts/7
>
> Referred link: https://bugs.launchpad.net/qemu/+bug/938552
>
> Reported-by: Craig Ringer <ringerc@gmail.com>
> Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
> ---
>  qemu-char.c |    4 +++-
>  1 files changed, 3 insertions(+), 1 deletions(-)
>
> diff --git a/qemu-char.c b/qemu-char.c
> index 242b799..2b0f5f4 100644
> --- a/qemu-char.c
> +++ b/qemu-char.c
> @@ -981,6 +981,7 @@ static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
>      CharDriverState *chr;
>      PtyCharDriver *s;
>      struct termios tty;
> +    char *label;
>      int master_fd, slave_fd, len;
>  #if defined(__OpenBSD__) || defined(__DragonFly__)
>      char pty_name[PATH_MAX];
> @@ -1006,7 +1007,8 @@ static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
>      chr->filename = g_malloc(len);
>      snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd));
>      qemu_opt_set(opts, "path", q_ptsname(master_fd));
> -    fprintf(stderr, "char device redirected to %s\n", q_ptsname(master_fd));
> +    label = g_strdup(qemu_opts_id(opts));
> +    fprintf(stderr, "char device %s redirected to %s\n", label, q_ptsname(master_fd));
>  
>      s = g_malloc0(sizeof(PtyCharDriver));
>      chr->opaque = s;

qemu_opts_id() may return null.  g_strdup() then also returns null.
Printing null with %s crashes on some systems.

You leak label.  Best fixed by not allocating in the first place.

Try this:

    label = qemu_opts_id(opts)
    fprintf(stderr, "char device%S%s redirected to %s\n",
            label ? " " : "",
            label ?: , "",
            q_ptsname(master_fd));

Don't forget to imporve your subject :)

Patch

diff --git a/qemu-char.c b/qemu-char.c
index 242b799..2b0f5f4 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -981,6 +981,7 @@  static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
     CharDriverState *chr;
     PtyCharDriver *s;
     struct termios tty;
+    char *label;
     int master_fd, slave_fd, len;
 #if defined(__OpenBSD__) || defined(__DragonFly__)
     char pty_name[PATH_MAX];
@@ -1006,7 +1007,8 @@  static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
     chr->filename = g_malloc(len);
     snprintf(chr->filename, len, "pty:%s", q_ptsname(master_fd));
     qemu_opt_set(opts, "path", q_ptsname(master_fd));
-    fprintf(stderr, "char device redirected to %s\n", q_ptsname(master_fd));
+    label = g_strdup(qemu_opts_id(opts));
+    fprintf(stderr, "char device %s redirected to %s\n", label, q_ptsname(master_fd));
 
     s = g_malloc0(sizeof(PtyCharDriver));
     chr->opaque = s;