Skip to contentSkip to conversation

pty/tty functions for BSD too

Qemu-devel Samuel Thibault 10 participants 18 messages Aug 18, 2008 – Aug 21, 2008

lore
Share & tools
lore
#0 lore raw

Hello,

In Xen, pty/tty functions are enabled for BSD too, shouldn't we enable them in upstream qemu too, as patched below?

Samuel

vl.c +2 -2
Index: vl.c
===================================================================
@@ -2466,7 +2466,7 @@
 }
 #endif
 
-#if defined(__linux__) || defined(__sun__)
+#if defined(__linux__) || defined(__sun__) || defined(__NetBSD__) || defined(__OpenBSD__)
 
 typedef struct {
     int fd;
@@ -3816,7 +3816,7 @@
         return qemu_chr_open_pp(filename);
     } else
 #endif
-#if defined(__linux__) || defined(__sun__)
+#if defined(__linux__) || defined(__sun__) || defined(__NetBSD__) || defined(__OpenBSD__)
     if (strstart(filename, "/dev/", NULL)) {
         return qemu_chr_open_tty(filename);
     } else
#1 lore raw
Quoted 5 lines · Samuel Thibault

Hello,

In Xen, pty/tty functions are enabled for BSD too, shouldn't we enable them in upstream qemu too, as patched below?

And you're sure that these functions compile/work on NetBSD/OpenBSD? If so, we can enable them.

Regards,

Anthony Liguori

Quoted 27 lines

Samuel

Index: vl.c
===================================================================

vl.c +2 -2
@@ -2466,7 +2466,7 @@
 }
 #endif
 
-#if defined(__linux__) || defined(__sun__)
+#if defined(__linux__) || defined(__sun__) || defined(__NetBSD__) || defined(__OpenBSD__)
 
 typedef struct {
     int fd;
@@ -3816,7 +3816,7 @@
         return qemu_chr_open_pp(filename);
     } else
 #endif
-#if defined(__linux__) || defined(__sun__)
+#if defined(__linux__) || defined(__sun__) || defined(__NetBSD__) || defined(__OpenBSD__)
     if (strstart(filename, "/dev/", NULL)) {
         return qemu_chr_open_tty(filename);
     } else
#2 lore raw
Quoted 6 lines · Anthony Liguori, le Mon 18 Aug 2008 09:06:41 -0500, a écrit :
Quoted 3 lines · Samuel Thibault

In Xen, pty/tty functions are enabled for BSD too, shouldn't we enable them in upstream qemu too, as patched below?

And you're sure that these functions compile/work on NetBSD/OpenBSD?

The defines are explicit in Xen, so I guess somebody tested it. I haven't myself. I wonder why there is no FreeBSD however.

Samuel

#3 lore raw

From: Samuel Thibault <[email protected]>
Subject: Re: [Qemu-devel] pty/tty functions for BSD too
Date: Mon, 18 Aug 2008 15:23:33 +0100

Quoted 10 lines
Quoted 6 lines · Anthony Liguori, le Mon 18 Aug 2008 09:06:41 -0500, a écrit :
Quoted 3 lines · Samuel Thibault

In Xen, pty/tty functions are enabled for BSD too, shouldn't we enable them in upstream qemu too, as patched below?

And you're sure that these functions compile/work on NetBSD/OpenBSD?

The defines are explicit in Xen, so I guess somebody tested it. I haven't myself. I wonder why there is no FreeBSD however.

The TTY/PTY code is 4.2BSD, so everybdoy has it... Not sure about why no FreeBSD. I haven't looked at the code, but I thought there was a different fork it took..

I've noticed many times in the past that the #ifdef sun and/or #ifdef linux code works great on FreeBSD.

Warner

#5 lore raw
In reply to Samuel Thibault
Quoted 7 lines · Samuel Thibault
Quoted 5 lines · Anthony Liguori, le Mon 18 Aug 2008 09:06:41 -0500, a écrit :
Quoted 3 lines · Samuel Thibault

In Xen, pty/tty functions are enabled for BSD too, shouldn't we enable them in upstream qemu too, as patched below?

Quoted 5 lines
Quoted 1 line

And you're sure that these functions compile/work on NetBSD/OpenBSD?

The defines are explicit in Xen, so I guess somebody tested it. I
haven't myself. I wonder why there is no FreeBSD however.

The patch does not work on OpenBSD, because while openpty() is available, ptsname() isn't.

I tested the attached version on OpenBSD and Linux, pty name is printed correctly.

vl.c +11 -4
Index: qemu/vl.c
===================================================================
@@ -109,6 +109,10 @@
 #include "libslirp.h"
 #endif
 
+#if defined(__NetBSD__) || defined(__OpenBSD__)
+#include <util.h>
+#endif
+
 #if defined(CONFIG_VDE)
 #include <libvdeplug.h>
 #endif
@@ -2467,7 +2471,8 @@
 }
 #endif
 
-#if defined(__linux__) || defined(__sun__)
+#if defined(__linux__) || defined(__sun__) || defined(__NetBSD__) \
+    || defined(__OpenBSD__)
 
 typedef struct {
     int fd;
@@ -2595,6 +2600,7 @@
     PtyCharDriver *s;
     struct termios tty;
     int slave_fd;
+    char pty_name[PATH_MAX];
 
     chr = qemu_mallocz(sizeof(CharDriverState));
     if (!chr)
@@ -2605,7 +2611,7 @@
         return NULL;
     }
 
-    if (openpty(&s->fd, &slave_fd, NULL, NULL, NULL) < 0) {
+    if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
         return NULL;
     }
 
@@ -2614,7 +2620,7 @@
     tcsetattr(slave_fd, TCSAFLUSH, &tty);
     close(slave_fd);
 
-    fprintf(stderr, "char device redirected to %s\n", ptsname(s->fd));
+    fprintf(stderr, "char device redirected to %s\n", pty_name);
 
     chr->opaque = s;
     chr->chr_write = pty_chr_write;
@@ -3817,7 +3823,8 @@
         return qemu_chr_open_pp(filename);
     } else
 #endif
-#if defined(__linux__) || defined(__sun__)
+#if defined(__linux__) || defined(__sun__) || defined(__NetBSD__) \
+    || defined(__OpenBSD__)
     if (strstart(filename, "/dev/", NULL)) {
         return qemu_chr_open_tty(filename);
     } else
#6 lore raw
Quoted 11 lines · Blue Swirl
Quoted 10 lines · Samuel Thibault
Quoted 6 lines · Anthony Liguori, le Mon 18 Aug 2008 09:06:41 -0500, a écrit :
Quoted 3 lines · Samuel Thibault

In Xen, pty/tty functions are enabled for BSD too, shouldn't we enable them in upstream qemu too, as patched below?

And you're sure that these functions compile/work on NetBSD/OpenBSD?

Quoted 8 lines
Quoted 2 lines

The defines are explicit in Xen, so I guess somebody tested it. I
haven't myself. I wonder why there is no FreeBSD however.

The patch does not work on OpenBSD, because while openpty() is available, ptsname() isn't.

I tested the attached version on OpenBSD and Linux, pty name is printed correctly.

Passing a non-NULL value to openpty()'s name parameter is not safe

[quote openpty(1)]
BUGS
Nobody knows how much space should be reserved for name. So, call- ing openpty() or forkpty() with non-NULL name may not be secure.
[/quote]

If BSD has no other way to determine the PTY name, then at least it should be conditionalized so that systems with ptsname() use it, only falling back to using the 'name' arg to openpty() for OS lacking ptsname

Regards,
Daniel

Signature · 4 lines
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
#7 lore raw
Quoted 22 lines · Daniel P. Berrange
Quoted 11 lines · Blue Swirl
Quoted 10 lines · Samuel Thibault
Quoted 6 lines · Anthony Liguori, le Mon 18 Aug 2008 09:06:41 -0500, a écrit :
Quoted 3 lines · Samuel Thibault

In Xen, pty/tty functions are enabled for BSD too, shouldn't we enable them in upstream qemu too, as patched below?

And you're sure that these functions compile/work on NetBSD/OpenBSD?

Quoted 8 lines
Quoted 2 lines

The defines are explicit in Xen, so I guess somebody tested it. I
haven't myself. I wonder why there is no FreeBSD however.

The patch does not work on OpenBSD, because while openpty() is available, ptsname() isn't.

I tested the attached version on OpenBSD and Linux, pty name is printed correctly.

Quoted 7 lines

Passing a non-NULL value to openpty()'s name parameter is not safe

[quote openpty(1)]
BUGS
Nobody knows how much space should be reserved for name. So, call- ing openpty() or forkpty() with non-NULL name may not be secure.
[/quote]

Maybe in theory, but in practice the name will be /dev/pty[0-9a-z][a-z] or /dev/pts/[0-9]* or something similar. Even if they are not, PATH_MAX should be enough.

Quoted 3 lines

If BSD has no other way to determine the PTY name, then at least it should be conditionalized so that systems with ptsname() use it, only falling back to using the 'name' arg to openpty() for OS lacking ptsname

I'm not convinced (yet?) this will be worth it.

#4 lore raw
In reply to Warner Losh
Quoted 19 lines · Warner Losh

From: Samuel Thibault <[email protected]>
Subject: Re: [Qemu-devel] pty/tty functions for BSD too
Date: Mon, 18 Aug 2008 15:23:33 +0100

Quoted 4 lines
Quoted 2 lines · Anthony Liguori, le Mon 18 Aug 2008 09:06:41 -0500, a écrit :

Samuel Thibault wrote:

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

In Xen, pty/tty functions are enabled for BSD too, shouldn't we enable them in upstream qemu too, as patched below?

And you're sure that these functions compile/work on NetBSD/OpenBSD?

The defines are explicit in Xen, so I guess somebody tested it. I haven't myself. I wonder why there is no FreeBSD however.

Quoted 7 lines

The TTY/PTY code is 4.2BSD, so everybdoy has it... Not sure about why no FreeBSD. I haven't looked at the code, but I thought there was a different fork it took..

I've noticed many times in the past that the #ifdef sun and/or #ifdef linux code works great on FreeBSD.

I hate to admit it, but autoconf really has it right here.

Instead of doing:

#if defined(__linux__) || defined(__FreeBSD__) || ... fd = openpty();
#endif

we should have:

#ifdef HAVE_openpty
fd = openpty();
#endif

and do a configure check to see if there is a usable openpty. It makes the code easier to maintain and you don't have to deal with all of this black magic wrt what individual OSes support.

Regards,

Anthony Liguori

Quoted 4 lines

Warner

#8 lore raw
In reply to Blue Swirl
Quoted 37 lines · Blue Swirl
Quoted 22 lines · Daniel P. Berrange
Quoted 11 lines · Blue Swirl
Quoted 10 lines · Samuel Thibault
Quoted 6 lines · Anthony Liguori, le Mon 18 Aug 2008 09:06:41 -0500, a écrit :
Quoted 3 lines · Samuel Thibault

In Xen, pty/tty functions are enabled for BSD too, shouldn't we enable them in upstream qemu too, as patched below?

And you're sure that these functions compile/work on NetBSD/OpenBSD?

Quoted 8 lines
Quoted 2 lines

The defines are explicit in Xen, so I guess somebody tested it. I
haven't myself. I wonder why there is no FreeBSD however.

The patch does not work on OpenBSD, because while openpty() is available, ptsname() isn't.

I tested the attached version on OpenBSD and Linux, pty name is printed correctly.

Quoted 7 lines

Passing a non-NULL value to openpty()'s name parameter is not safe

[quote openpty(1)]
BUGS
Nobody knows how much space should be reserved for name. So, call- ing openpty() or forkpty() with non-NULL name may not be secure.
[/quote]

Maybe in theory, but in practice the name will be
/dev/pty[0-9a-z][a-z] or /dev/pts/[0-9]* or something similar. Even if they are not, PATH_MAX should be enough.

Quoted 6 lines
Quoted 3 lines

If BSD has no other way to determine the PTY name, then at least it should be conditionalized so that systems with ptsname() use it, only falling back to using the 'name' arg to openpty() for OS lacking ptsname

I'm not convinced (yet?) this will be worth it.

Glibc uses an internal PATH_MAX buffer. If the name does not fit, the buffer will be doubled in size:
http://sourceware.org/cgi-bin/cvsweb.cgi/libc/login/openpty.c?rev=1.8&content-type=text/x-cvsweb-markup&cvsroot=glibc

But in practice the pty will be allocated using getpt(), which uses hard coded ptmx name:
http://sourceware.org/cgi-bin/cvsweb.cgi/libc/sysdeps/unix/sysv/linux/getpt.c?rev=1.10&content-type=text/x-cvsweb-markup&cvsroot=glibc

And ptsname() is used to get the pty name, which uses either hard coded buffer length of _PATH_DEVPTS or _PATH_TTY:
http://sourceware.org/cgi-bin/cvsweb.cgi/libc/sysdeps/unix/sysv/linux/ptsname.c?rev=1.15&content-type=text/x-cvsweb-markup&cvsroot=glibc

OpenBSD forces the name to be in format "/dev/ptyXX":
http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libutil/pty.c?rev=1.15&content-type=text/x-cvsweb-markup

NetBSD is similar, except also /dev/ttyXX is possible:
http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libutil/pty.c?rev=1.29&content-type=text/x-cvsweb-markup

I still maintain that in reality, PATH_MAX should be enough.

#9 lore raw
Quoted 34 lines · Blue Swirl
Quoted 14 lines · Blue Swirl
Quoted 6 lines · Daniel P. Berrange

[quote openpty(1)]
BUGS
Nobody knows how much space should be reserved for name. So, call- ing openpty() or forkpty() with non-NULL name may not be secure.
[/quote]

Maybe in theory, but in practice the name will be
/dev/pty[0-9a-z][a-z] or /dev/pts/[0-9]* or something similar. Even if they are not, PATH_MAX should be enough.

Quoted 6 lines
Quoted 3 lines

If BSD has no other way to determine the PTY name, then at least it should be conditionalized so that systems with ptsname() use it, only falling back to using the 'name' arg to openpty() for OS lacking ptsname

I'm not convinced (yet?) this will be worth it.

Glibc uses an internal PATH_MAX buffer. If the name does not fit, the buffer will be doubled in size:
http://sourceware.org/cgi-bin/cvsweb.cgi/libc/login/openpty.c?rev=1.8&content-type=text/x-cvsweb-markup&cvsroot=glibc

But in practice the pty will be allocated using getpt(), which uses hard coded ptmx name:
http://sourceware.org/cgi-bin/cvsweb.cgi/libc/sysdeps/unix/sysv/linux/getpt.c?rev=1.10&content-type=text/x-cvsweb-markup&cvsroot=glibc

And ptsname() is used to get the pty name, which uses either hard coded buffer length of _PATH_DEVPTS or _PATH_TTY:
http://sourceware.org/cgi-bin/cvsweb.cgi/libc/sysdeps/unix/sysv/linux/ptsname.c?rev=1.15&content-type=text/x-cvsweb-markup&cvsroot=glibc

Quoted 7 lines

OpenBSD forces the name to be in format "/dev/ptyXX":
http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libutil/pty.c?rev=1.15&content-type=text/x-cvsweb-markup

NetBSD is similar, except also /dev/ttyXX is possible:
http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libutil/pty.c?rev=1.29&content-type=text/x-cvsweb-markup

I still maintain that in reality, PATH_MAX should be enough.

Ok, I guess its safe enough in practice then.

Regards,
Daniel

Signature · 4 lines
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
#10 lore raw
In reply to Blue Swirl
Quoted 9 lines · Blue Swirl

And ptsname() is used to get the pty name, which uses either hard coded buffer length of _PATH_DEVPTS or _PATH_TTY:
http://sourceware.org/cgi-bin/cvsweb.cgi/libc/sysdeps/unix/sysv/linux/ptsname.c?rev=1.15&content-type=text/x-cvsweb-markup&cvsroot=glibc

OpenBSD forces the name to be in format "/dev/ptyXX":
http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libutil/pty.c?rev=1.15&content-type=text/x-cvsweb-markup

NetBSD is similar, except also /dev/ttyXX is possible:
http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libutil/pty.c?rev=1.29&content-type=text/x-cvsweb-markup

Other platforms use slightly longer names, but I've never heard of one that wouldn't fit into PATH_MAX - assuming PATH_MAX is sane.

I'd still use ptsname()/ptsname_r() when available though.

-- Jamie

#12 lore raw
Quoted 10 lines · Blue Swirl
Quoted 7 lines

Glibc uses an internal PATH_MAX buffer. If the name does not fit, the
buffer will be doubled in size:
http://sourceware.org/cgi-bin/cvsweb.cgi/libc/login/openpty.c?rev=1.8&content-type=text/x-cvsweb-markup&cvsroot=glibc

Quoted 1 line
Quoted 1 line

But in practice the pty will be allocated using getpt(), which uses

They, too, should switch to posix_openpt(), fwiw.

Please don't start dictating what an OS should have or not, or this will turn into ffmpeg-like porting flames.
It's not about porting all OSes to QEMU, but porting QEMU to OSes :P

François.

#13 lore raw
Quoted 7 lines · François Revol
Quoted 7 lines · Blue Swirl
Quoted 6 lines

Glibc uses an internal PATH_MAX buffer. If the name does not fit, the
buffer will be doubled in size:
http://sourceware.org/cgi-bin/cvsweb.cgi/libc/login/openpty.c?rev=1.8&content-type=text/x-cvsweb-markup&cvsroot=glibc

Quoted 1 line
Quoted 1 line
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

But in practice the pty will be allocated using getpt(), which uses

They, too, should switch to posix_openpt(), fwiw.

Please don't start dictating what an OS should have or not, or this

I'm not talking about an OS but a libc that should try to use standard conforming functions where possible. Whether or not they provide the non-standard getpt() for legacy applications is not the question.

A libc that is POSIX.1-2001 compliant has to provide posix_openpt() anyway, thus it doesn't make too much sense to use the non-standard getpt() but i guess nobody cared enough to adjust glibc yet.

Quoted 2 lines

will turn into ffmpeg-like porting flames.
It's not about porting all OSes to QEMU, but porting QEMU to OSes :P

Well, i talked about glibc interna, not QEMU interna, so my comment was a bit OT, admittedly ;)

#17 lore raw
In reply to Daniel P. Berrange
Quoted 1 line

From the OpenBSD man page...

DESCRIPTION
The openpty(), login_tty(), and forkpty() functions perform manipulations on ttys and pseudo-ttys.

The openpty() function finds an available pseudo-tty and returns file de- scriptors for the master and slave in amaster and aslave. If name is non-null, the filename of the slave is returned in name (a string of at least 16 characters). If termp is non-null, the terminal parameters of the slave will be set to the values in termp. If winp is non-null, the window size of the slave will be set to the values in winp.

The openpty() function works in the following way: first it attempts to allocate the pseudo-tty through the /dev/ptm device (see pty(4) for de- tails) and if that fails it searches for a free pseudo-tty by iterating through all existing pseudo-tty devices in /dev. When a free pseudo-tty is found, its ownership is changed to the UID of the caller, permissions are set to correct values, and all earlier uses of that device are re- voked (see revoke(2) for details). The first method can work for any us- er, the second method requires super-user privileges in most cases.

In OpenBSD, it's a pretty safe bet that 16chars is overkill. The tty and/or pty name will never be longer than /dev/?ty??.

Thanks,

Signature · 14 lines
Todd Fries .. [email protected]

 _____________________________________________
|                                             \  1.636.410.0632 (voice)
| Free Daemon Consulting, LLC                 \  1.405.227.9094 (voice)
| http://FreeDaemonConsulting.com             \  1.866.792.3418 (FAX)
| "..in support of free software solutions."  \          250797 (FWD)
|                                             \
 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
                                                 
              37E7 D3EB 74D0 8D66 A68D  B866 0326 204E 3F42 004A
                        http://todd.fries.net/pgp.txt

Penned by Daniel P. Berrange on 20080818 17:26.18, we have:
| On Mon, Aug 18, 2008 at 07:16:36PM +0300, Blue Swirl wrote:
| > On 8/18/08, Samuel Thibault <[email protected]> wrote:
| > > Anthony Liguori, le Mon 18 Aug 2008 09:06:41 -0500, a ?crit :
| > >
| > > > Samuel Thibault wrote:
| > >  > >In Xen, pty/tty functions are enabled for BSD too, shouldn't we enable
| > >  > >them in upstream qemu too, as patched below?
| > >  > >
| > >  >
| > >  > And you're sure that these functions compile/work on NetBSD/OpenBSD?
| > >
| > >
| > > The defines are explicit in Xen, so I guess somebody tested it.  I
| > >  haven't myself.  I wonder why there is no FreeBSD however.
| > 
| > The patch does not work on OpenBSD, because while openpty() is
| > available, ptsname() isn't.
| > 
| > I tested the attached version on OpenBSD and Linux, pty name is
| > printed correctly.
| 
| Passing a non-NULL value to openpty()'s name parameter is not safe
| 
| [quote openpty(1)]
| BUGS
|        Nobody knows how much space should be reserved for name.  So, call-
|        ing openpty() or forkpty() with non-NULL name may not be secure.
| [/quote]
| 
| If BSD has no other way to determine the PTY name, then at least it
| should be conditionalized so that systems with ptsname() use it, only
| falling back to using the 'name' arg to openpty() for OS lacking ptsname
| 
| Regards,
| Daniel
| -- 
| |: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
| |: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
| |: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
| |: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
| 
#14 lore raw
In reply to Bernhard Reutner-Fischer
Quoted 3 lines · Bernhard Reutner-Fischer, le Tue 19 Aug 2008 13:40:29 +0200, a écrit :

A libc that is POSIX.1-2001 compliant has to provide posix_openpt() anyway, thus it doesn't make too much sense to use the non-standard getpt() but i guess nobody cared enough to adjust glibc yet.

Err, glibc does have posix_openpt, in getpt.c

Samuel

#15 lore raw
Quoted 1 line · Samuel Thibault

Err, glibc does have posix_openpt, in getpt.c

As have FreeBSD and NetBSD in their currently supported releases (6/7 for FreeBSD, 3/4 for NetBSD).

ciao
Klaus

#16 lore raw
Quoted 7 lines · Klaus Heinz
Quoted 1 line · Samuel Thibault

Err, glibc does have posix_openpt, in getpt.c

As have FreeBSD and NetBSD in their currently supported releases (6/7
for FreeBSD, 3/4 for NetBSD).

Here's an updated patch that avoids ptsname only on OpenBSD and enables pty also for FreeBSD.

vl.c +17 -4
Index: qemu/vl.c
===================================================================
@@ -109,6 +109,10 @@
 #include "libslirp.h"
 #endif
 
+#if defined(__OpenBSD__)
+#include <util.h>
+#endif
+
 #if defined(CONFIG_VDE)
 #include <libvdeplug.h>
 #endif
@@ -2467,7 +2471,8 @@
 }
 #endif
 
-#if defined(__linux__) || defined(__sun__)
+#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
+    || defined(__NetBSD__) || defined(__OpenBSD__)
 
 typedef struct {
     int fd;
@@ -2595,6 +2600,13 @@
     PtyCharDriver *s;
     struct termios tty;
     int slave_fd;
+#if defined(__OpenBSD__)
+    char pty_name[PATH_MAX];
+#define q_ptsname(x) pty_name
+#else
+    char *pty_name = NULL;
+#define q_ptsname(x) ptsname(x)
+#endif
 
     chr = qemu_mallocz(sizeof(CharDriverState));
     if (!chr)
@@ -2605,7 +2617,7 @@
         return NULL;
     }
 
-    if (openpty(&s->fd, &slave_fd, NULL, NULL, NULL) < 0) {
+    if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
         return NULL;
     }
 
@@ -2614,7 +2626,7 @@
     tcsetattr(slave_fd, TCSAFLUSH, &tty);
     close(slave_fd);
 
-    fprintf(stderr, "char device redirected to %s\n", ptsname(s->fd));
+    fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
 
     chr->opaque = s;
     chr->chr_write = pty_chr_write;
@@ -3817,7 +3829,8 @@
         return qemu_chr_open_pp(filename);
     } else
 #endif
-#if defined(__linux__) || defined(__sun__)
+#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
+    || defined(__NetBSD__) || defined(__OpenBSD__)
     if (strstart(filename, "/dev/", NULL)) {
         return qemu_chr_open_tty(filename);
     } else