Skip to contentSkip to conversation

create qemu_openpty_raw() helper function and move it to a separate file

PATCH In review Michael Tokarev 2 participants 2 messages Jun 13, 2013

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 create qemu_openpty_raw() helper function and move it to a separate file

In two places qemu uses openpty() which is very system-dependent, and in both places the pty is switched to raw mode as well. Make a wrapper function which does both steps, and move all the system-dependent complexity into a separate file, together with static/local implementations of openpty() and cfmakeraw() from qemu-char.c.

It is in a separate file, not part of oslib-posix.c, because openpty() often resides in -lutil which is not linked to every program qemu builds.

This change removes #including of <pty.h>, <termios.h> and other rather specific system headers out of qemu-common.h, which isn't a place for such specific headers really.

This version has been verified to build correctly on Linux, OpenBSD, FreeBSD and OpenIndiana. On the latter it lets qemu to be built with gtk gui which were not possible there due to missing openpty() and cfmakeraw().

---
Changes since v1:

- added a forgotten #include <termios.h> for *BSD, which was recently added into qemu-common.h by Brad Smith, and which I intended to use in qemu-openpty.c too, but somehow forgot.

Changes since v2:

- verified build on solaris (openindiana) and added #include <termios.h> there too,

- removed trailing newline

This is trivial enough to warrant a -trivial tree. The diffstat below is large because of the comments in the new file.

 include/qemu-common.h |   15 +-----
 qemu-char.c           |   77 ++--------------------------
 ui/gtk.c              |   12 ++---
 util/Makefile.objs    |    2 +-
 util/qemu-openpty.c   |  135 +++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 146 insertions(+), 95 deletions(-)
 create mode 100644 util/qemu-openpty.c
include/qemu-common.h +2 -13
index cb82ef3..3b1ca8e 100644
@@ -42,19 +42,6 @@
 #include <signal.h>
 #include "glib-compat.h"
 
-#if defined(__GLIBC__)
-# include <pty.h>
-#elif defined CONFIG_BSD
-# include <termios.h>
-# if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
-#  include <libutil.h>
-# else
-#  include <util.h>
-# endif
-#elif defined CONFIG_SOLARIS
-# include <stropts.h>
-#endif
-
 #ifdef _WIN32
 #include "sysemu/os-win32.h"
 #endif
@@ -232,6 +219,8 @@ ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags)
 
 #ifndef _WIN32
 int qemu_pipe(int pipefd[2]);
+/* like openpty() but also makes it raw; return master fd */
+int qemu_openpty_raw(int *aslave, char *pty_name);
 #endif
 
 #ifdef _WIN32
qemu-char.c +5 -72

102 lines not on this page · raw message

ui/gtk.c +3 -9
index 52c3f95..3405df6 100644
@@ -1156,8 +1156,7 @@ static GSList *gd_vc_init(GtkDisplayState *s, VirtualConsole *vc, int index, GSL
     GIOChannel *chan;
     GtkWidget *scrolled_window;
     GtkAdjustment *vadjustment;
-    int master_fd, slave_fd, ret;
-    struct termios tty;
+    int master_fd, slave_fd;
 
     snprintf(buffer, sizeof(buffer), "vc%d", index);
     snprintf(path, sizeof(path), "<QEMU>/View/VC%d", index);
@@ -1177,13 +1176,8 @@ static GSList *gd_vc_init(GtkDisplayState *s, VirtualConsole *vc, int index, GSL
 
     vc->terminal = vte_terminal_new();
 
-    ret = openpty(&master_fd, &slave_fd, NULL, NULL, NULL);
-    g_assert(ret != -1);
-
-    /* Set raw attributes on the pty. */
-    tcgetattr(slave_fd, &tty);
-    cfmakeraw(&tty);
-    tcsetattr(slave_fd, TCSAFLUSH, &tty);
+    master_fd = qemu_openpty_raw(&slave_fd, NULL);
+    g_assert(master_fd != -1);
 
 #if VTE_CHECK_VERSION(0, 26, 0)
     pty = vte_pty_new_foreign(master_fd, NULL);
util/Makefile.objs +1 -1
index 4a1bd4e..dc72ab0 100644
@@ -1,6 +1,6 @@
 util-obj-y = osdep.o cutils.o unicode.o qemu-timer-common.o
 util-obj-$(CONFIG_WIN32) += oslib-win32.o qemu-thread-win32.o event_notifier-win32.o
-util-obj-$(CONFIG_POSIX) += oslib-posix.o qemu-thread-posix.o event_notifier-posix.o
+util-obj-$(CONFIG_POSIX) += oslib-posix.o qemu-thread-posix.o event_notifier-posix.o qemu-openpty.o
 util-obj-y += envlist.o path.o host-utils.o cache-utils.o module.o
 util-obj-y += bitmap.o bitops.o hbitmap.o
 util-obj-y += fifo8.o
util/qemu-openpty.c new file +135 -0

135 lines not on this page · raw message

Signature · 1 line
1.7.10.4
Tested-by on v3 1/1
#1 lore raw

Am 13.06.2013 18:04, schrieb Michael Tokarev:

Quoted 29 lines

In two places qemu uses openpty() which is very system-dependent, and in both places the pty is switched to raw mode as well. Make a wrapper function which does both steps, and move all the system-dependent complexity into a separate file, together with static/local implementations of openpty() and cfmakeraw() from qemu-char.c.

It is in a separate file, not part of oslib-posix.c, because openpty() often resides in -lutil which is not linked to every program qemu builds.

This change removes #including of <pty.h>, <termios.h> and other rather specific system headers out of qemu-common.h, which isn't a place for such specific headers really.

This version has been verified to build correctly on Linux, OpenBSD, FreeBSD and OpenIndiana. On the latter it lets qemu to be built with gtk gui which were not possible there due to missing openpty() and cfmakeraw().

---
Changes since v1:

- added a forgotten #include <termios.h> for *BSD, which was recently added into qemu-common.h by Brad Smith, and which I intended to use in qemu-openpty.c too, but somehow forgot.

Quoted 6 lines

Changes since v2:

- verified build on solaris (openindiana) and added #include <termios.h> there too,

- removed trailing newline

on OpenIndiana 151a.

Thanks,
Andreas

Quoted 11 lines

This is trivial enough to warrant a -trivial tree. The diffstat below is large because of the comments in the new file.

 include/qemu-common.h |   15 +-----
 qemu-char.c           |   77 ++--------------------------
 ui/gtk.c              |   12 ++---
 util/Makefile.objs    |    2 +-
 util/qemu-openpty.c   |  135 +++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 146 insertions(+), 95 deletions(-)
 create mode 100644 util/qemu-openpty.c